Home » Java programming language

Java File Class boolean createNewFile() method with Example

Java File Class boolean createNewFile() method: Here, we are going to learn about the boolean createNewFile() method of File class with its syntax and example.
Submitted by Preeti Jain, on July 05, 2019

File Class boolean createNewFile()

  • This method is available in package
  • This method is used to create a new file by using createNewFile() method and this method is accessible with the File object.
  • If a file is already exists i.e. the name of created file is already exists that means we are not allowed to create a file of same name.
  • The return type of this method is Boolean i.e. it returns true or false if true that means file is successfully created and returns false that means the file already exists.


    boolean createNewFile(){


We don't pass any object as a parameter in the method of the File.

Return value:

The return type of this method is Boolean i.e. it returns true or false if true that means file is successfully created else returns false that means the file already exists.

Java program to demonstrate example of createNewFile() method

// import the File class because we will use File class methods

// import the Exception class because it may raise an exception 
// when working with files
import java.lang.Exception;

public class CreateFile {
    public static void main(String[] args) {
        try {
            // Specify the path of file and we use double slashes 
            // to escape '\' character sequence for windows otherwise 
            // it will be considerable as url.
            File file1 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\javafiles.txt");
            File file2 = new File("C:\\Users\\computer clinic\\OneDrive\\Articles\\myjava.txt");

            // By using createNewFile()create a new file named 
            // javafiles.txt because file is not exists 
            // (i.e it returns true) so this method creates empty file.
            if (file1.createNewFile())
                System.out.println("File created Successfully" + " " + file1.getName());
                System.out.println("File already exists " + file1.getName());

            // By using createNewFile() is not create a new file 
            // named myjava.txt because the name of this file is 
            // already exists(i.e. it returns false) so we will 
            // get a message File already exists .
            if (file2.createNewFile())
                System.out.println("File created Successfully" + file2.getName());
                System.out.println("File already exists" + " " + file2.getName());

        } catch (Exception e) {
            System.out.println("An error occurred.");


D:\Programs>java CreateFile
File already exists C:\Users\computer clinic\OneDrive\Articles\javafiles.txt
File already exists C:\Users\computer clinic\OneDrive\Articles\myjava.txt


Comments and Discussions



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© some rights reserved.