Tuesday 4 August 2015

Create file if it does not exist Using Java

The following code 'll create a file 'examplefile.txt',if doesn't exist

    try {

     File file = new File("examplefile.txt");
     // Create file if it does not exist
     boolean success = file.createNewFile();
     if (success) {
     // File did not exist and was created
     } else {
     // File already exists
     }
    } catch (IOException e) {
    e.printStackTrace();
    }

No comments:

Post a Comment