convert a string to a directory name

10,914

To create a directory use the following code. Note the use of .mkdir() method and the double backslash.

In your code I would bet System.out.println(new File(dir).isFile() would return true.

 import java.io.File;

 public class MakeDirectory {

 public static void main(String[] args) {
    File f = new File("c:\\MyFolder");
    f.mkdir();

  }
 }
Share:
10,914
leba-lev
Author by

leba-lev

Programming Enthusiast!

Updated on June 04, 2022

Comments

  • leba-lev
    leba-lev almost 2 years

    Here is my case:

    String dir = "C:/root/dir1/";
    for(File f: new File(dir).listFiles()) {
    
    }
    

    On executing:

    System.out.println(new File(dir).isDirectory());
    System.out.println(new File(dir));
    

    gives

    false
    root/dir1
    

    The path stored in the string is valid. On converting the string to file type it looses the separator; how can the directory name be preserved?

    Thank you.