How to create a file in Java if one doesn't exists

The File.createNewFile() method creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. This methods return a true value if the file is created successfully and false if the file already exists or the operation failed.

package com.as400samplecode;


public class CreateFile {

 public static void main(String[] args) {
  try {

   File myFile = new File("data/newFile.txt");

   if (myFile.createNewFile()){
    System.out.println("File is created!");
    System.out.println("File already exists.");

  } catch (IOException e) {



No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.