0 Replies - 311 Views - Last Post: 18 November 2007 - 07:24 AM

#1 William_Wilson   User is offline

  • lost in compilation
  • member icon

Reputation: 207
  • View blog
  • Posts: 4,812
  • Joined: 23-December 05

Create Missing Directories

Posted 18 November 2007 - 07:24 AM

Description: Pass a String to the method which has all folders that are needed for saving a file. The folders which already exist will remain intact, while creating those which do not exist. If you are passing the path of a filename, use true, to avoid creating a folder of the name of the file, otherwise pass false.Creates all necessary directories in the path hierarchy supplied.
	//pass a File object
	public boolean createDirectories(String path, boolean isFile)
	{
		return createDirectories(new File(path),isFile);
	}
	//pass a String of a file name
	public boolean createDirectories(File dir, boolean isFile)
	{
		if(isFile)
			dir = dir.getParentFile(); //used if the path is of a filename
		if(!dir.exists()){
			dir.mkdirs();
			return true; //at least 1 new directory was created
		}
		return false; //no directories were added
	}
	public static void main(String args[])
	{
		test t = new test();
		t.createDirectories("C:\atest\new\test.tex",true);
	}
/*
Examples:
createDirectories("C:\test\new",false); //creates path C:testnew
createDirectories("C:\test\new\file.txt",false); //creates path C:testnewfile.txt
createDirectories("C:\test\new\file.txt",true); //creates path C:testnew
*/


Is This A Good Question/Topic? 0
  • +

Page 1 of 1