How to create a catalog file with the File class Java

  File class to create a directory

  Create a directory is used to when a new data when necessary to save certain file or picture to the local time, you need a folder filled, this time in order to preserve success, with or without prior created a folder will use a judge sentences judgment we need to save the directory path exists, if there is to be saved directly, if you create a directory does not exist.

  A test case:

  public class test4 {

  public static void main(String[] args) {

  File dir = new File("D:/test");

  if (! dir.exists ()) {// determines whether a directory exists

  dir.mkdir ();

  }

  }

  Create a perfect success, and now we are creating a parent directory, then I now need a multi-directory, multi-layered I then try to change the path, there are two cases Oh, the first one is, if you have With the catalog of the first layer, then create a directory on the inside, the second does not exist, first create two directories.

  The first:

  You can create a successful, two-story directory, or a directory is actually created it, all can successfully created.

  public static void main(String[] args) {

  Date datetime=new Date();

  File dir = new File("D:/test/test1");

  if (! dir.exists ()) {// determines whether a directory exists

  dir.mkdir ();

  System.out.println ( "performed" + datetime);

  }

  }

  

       The second: In the D drive is not present in the directory, you need to create two-story directory

  File dir = new File("D:/test1/test2");

  if (! dir.exists ()) {// determines whether a directory exists

  dir.mkdir ();

  System.out.println ( "- .. performed");

  } Wuxi ××× hospital http://www.zzch120.com/

  Code has been executed did not create the required directory to the second floor. After testing some computers write is no problem, the normal create, how you create can not do it? Calls another

  public class test4 {

  public static void main(String[] args) {

  Date datetime=new Date();

  File dir = new File("D:/test1/test2");

  if (! dir.exists ()) {// determines whether a directory exists

  //dir.mkdir();

  dir.mkdirs (); // multi-layer directory need to call mkdirs

  System.out.println ( "performed" + datetime);

  }

  }

  A two-story directories are created successful.


Guess you like

Origin blog.51cto.com/14335413/2404736