How to create a file in Java?

File createNewFile() method returns true if new file is created and false if file already exists.

Create New File in Specified Path

package com.nourit.javaIO;

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

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

      File file = new File("D:\\nour-it.txt");

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

    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}

 

NOUR-IT

IT Training Center for Java and Web technology. If you enjoy and like our tutorials and believe in our mission, Please Subscribe us on Youtube and follow us on Twitter, or befriend us on Facebook or Google Plus.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

*