Senin, 29 Februari 2016

How To Create An Empty File In Java I/O

Following the Java I/O code example how to create an empty file. We use File.createNewFile method to create a file. This method return a boolean value. Return true if the file is successfully created, and return false if the file already exists.


Source Code


File Name : CreateEmptyFileDemo.java

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

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

   File file = new File("c:\\test.txt");

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

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

Output


File test.txt is successfully created!


Tidak ada komentar:

Posting Komentar