Create new empty file with java



User Rating:  / 23
PoorBest 
Details
Category: Java
Created on Friday, 16 November 2012 10:22
Last Updated on Friday, 16 November 2012 10:22
Published on Friday, 16 November 2012 10:22
Written by Administrator
Hits: 35542

simple code for create new empty file :

View source
package com.developerpages.snippets.core;
 
import java.io.File;
import java.io.IOException;
 
public class CreateNewEmptyFile {
 
    public static void main(String[] args) {
        File file = new File("C://test.txt");
        boolean fileCreated = false;
        try {
            fileCreated = file.createNewFile();
        } catch (IOException ioe) {
            System.out.println("Error while creating empty file: " + ioe);
        }
 
        if (fileCreated) {
            System.out.println("Created empty file: " + file.getPath());
        } else {
            System.out.println("Failed to create empty file: " + file.getPath());
        }
 
    }
}