Java program to create a new file

File class is used for file related operations, in this program we will use createNewFile() method of File class that will return boolean value, if file is created successfully, method will retutn true otherwise it will return false.

Create New File using Java Program

// Java program to create new file


public class CreateFile {
  public static void main(String args[]) {
    final String fileName = "file1.txt";

    try {
      File objFile = new File(fileName);
      if (objFile.createNewFile()) {
        System.out.println("File created successfully.");
      } else {
        System.out.println("File creation failed!!!");
    } catch (Exception Ex) {
      System.out.println("Exception : " + Ex.toString());


File created successfully.

Java File Handling Programs »

Comments and Discussions!

Load comments ↻

Copyright © 2024 All rights reserved.