Skip to content

Instantly share code, notes, and snippets.

@azakordonets
Created May 22, 2014 09:23
Show Gist options
  • Select an option

  • Save azakordonets/ff758e856d3823d9089b to your computer and use it in GitHub Desktop.

Select an option

Save azakordonets/ff758e856d3823d9089b to your computer and use it in GitHub Desktop.
This method allow you to create file in specific folder under your project root. If you want to to save it somewhere on your computer, you can use "user.home" instead of "user.dir"
// get System properties :
Properties properties = System.getProperties();
// get Operating System home directory
String home = properties.get("user.dir").toString();
// get Operating System separator
String separator = properties.get("file.separator").toString();
// your directory name
String directoryName = "directoryName";
// your file name
String fileName = "filename";
// create your directory Object (wont harm if it is already there ...
// just an additional object on the heap that will cost you some bytes
File dir = new File(home+separator+directoryName);
// create a new directory, will do nothing if directory exists
dir.mkdir();
// create your file Object
File output = new File(dir,fileName);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment