How to check if a directory exists and create the directory if not exist in Java

1 Answer

0 votes
package javaapplication1;

import java.io.File;
import java.io.IOException;

public class JavaApplication1 {

    public static void main(String[] args) throws IOException {

        try {

            File dir = new File("d:\\tmp-old");

            if (dir.exists()) 
                System.out.println("directory exist");
            else
            {
                boolean b = dir.mkdir();
                if (b)
                    System.out.println("directory created");
                else
                    System.out.println("directory not created");
            }
            

        } catch (Exception e) {
            System.out.print(e.toString());
        }
    }
}

/*
           
run:
     
directory created
  
 */

 



answered Nov 19, 2016 by avibootz

Related questions

1 answer 126 views
1 answer 137 views
1 answer 179 views
1 answer 145 views
145 views asked Nov 17, 2016 by avibootz
1 answer 98 views
2 answers 301 views
...