Tuesday, March 8, 2016

Java I/O Examples : How To Create An Empty File

Intro

You can use createNewFile method to create an empty file. This method returns a boolean value. Return true if the file is successfully created, and return false is the file already exists.

Examples


package com.freesamplecode.java.io;

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

public class CreateEmptyFileDemo {
 public static void main(String[] args){
  File file = new File("newfile.txt");
  
  // create new file using createNewFile() method
  try {
   if(file.createNewFile()){
    System.out.println("File successfully created");
   }else{
    System.out.println("File already exists");
   }
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 }
}

Ouput


File successfully created

Screen Shoot

Java IO Examples - Create Empty File

Related Posts:

0 comments: