Create new empty file with java
Details
- 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: 40299
simple code for create new empty file :
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());
}
}
}