Wednesday, September 9, 2009

Java Code to Create a File

This Java Blog is today picking the topic of how to create File in Java. Java Code Online hopes that the article will be helpful to you all. In Java there are various possibilities for File Manipulation and handling. I suppose the simplest of them is regarding creation of a File. There are cases when we need to create a File for backing up some data, or write some important data, or might be some other reason. For all of the above we need to know first of all, how to create a File.

Creating a file is a simple process, and it uses just two methods of the File Class which is in the java.io package. They are:-

1. exists()
2. createNewFile()

The exists() method checks that whether the file exists or not. The createNewFile() method creates new File. Though creation of the File can be easily done by the createNewFile() method, it is a good practice to check that the file exists prior to creation or not. This case is important in scenarios where we need to create a File in case it does not exists, if it exists, then we need to modify it. So here the exists() method is very important, as it checks beforehand that the File exists or not.

The Java Code discussed today starts by declaring a new File at a particular path. You need to configure the path according to your application. I suppose that this is the only change apart from the package name, that you may need to do in this program, so as to compile and run it successfully.

The File is then tested for its existence using the method exists(), and if the file is found to be non existent, then a new File is created using createNewFile() method.

The Java Code is provide below for your reference:-

/////////////////////////////////////////

package developer;

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

public class CreateFile {

public static void main(String[] args) throws IOException {

File fileTest = new File("D:\\JavaCodeFile\\CreateTest.txt");

//Check for the existence of the file
System.out.println("Does the file exists before execution of the code? ");
System.out.println(fileTest.exists());

if(!fileTest.exists())
{
//Create a new file at the specified path
fileTest.createNewFile();
}

//Check again for the existence of the file
System.out.println("Does the file exists after execution of the code? ");
System.out.println(fileTest.exists());
}
}

/////////////////////////////////////////

The output on my console after compiling the application using the Java compiler and running this program on my JVM was as below:-

/////////////////////////////////////////

Does the file exists before execution of the code?
false
Does the file exists after execution of the code?
true

/////////////////////////////////////////

Hope that now you can easily create a file in Java. Do post comments if you have issues or if you liked the article. Java Code Online is working on new articles for Java lovers.

Related articles that might be of interest:-
Java Code to Read a File
Java Code to Write to a File
Java Code for File Extension
Java Code for Removing File Extension

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.