C C++ Java Data Structure Python JavaScript CSS Ajax PL/SQL PHP Puzzles C programs C++ programs Java programs

Home » Java programming language

Basics of File Handling in Java with Example

Learn: Basics of File Handling in Java, in this article we will learn how to Create a new Text File? How to write in the newly created text file? How to Read the Contents of newly Written text file? How to Delete the created the file?
Submitted by Mayank Singh, on June 20, 2017

File Handling is one of the most used and most important application of Programming Languages, As Java deals with Objects and Classes, a File can be considered to be an Object and for creating a File Object we have inbuilt Classes, out of the many Classes available to Read and Write a File Object in Java, we will consider the File Class under the Package name.

import Java.io.File;

File Class is an Abstract Representation of the Pathnames in the Operating System, these Pathnames are nothing but Strings used for representing Directories or File Names, Paths are of two types:

1) Absolute Path: It is the actual Directory where the file is stored, ex:

C:\Program Files\Java\jdk1.8.0_131\bin\Mayank.txt

Note: Microsoft Windows Operating System uses Back Slash (\) for representation of directories, while other Operating Systems uses Forward Slash ( / ).

2) Relative Path: It needs to be combined with another path in order to access our file. Ex:

jdk1.8.0_131\bin\Mayank.txt

Note: While representing Directory in Java Code, we will have to use two Back Word Slash \\ in case of MS Windows, or two Forward Slash // in case of other Operating Systems, reason being Java Considers Single Slash as an Escape Sequence.

ERROR MESSAGE on using Single Slash Character: illegal escape character.

Constructors in File Class

i) File(File parent, String child)

Creates a new File instance from a parent abstract pathname and a child pathname string.

ii) File(String pathname)

Creates a new File instance by converting the given pathname string into an abstract pathname.

iii) File(String parent, String child)

Creates a new File instance from a parent pathname string and a child pathname string.

iv) File(URI uri)

Creates a new File instance by converting the given file: URI into an abstract pathname.

Consider the program:

import java.util.*;
import java.io.*;

class RwFile
{
	public static void main(String args[])
	{
		Scanner KB=new Scanner(System.in);
		try
		{
			System.out.println("Enter the Message you wish to Print in the file");
			String S=KB.nextLine();
			File F=new File("C:\\Program Files\\Java\\jdk1.8.0_131\\bin\\SuperMachines.txt");
			if(F.createNewFile())
			{
				System.out.println("File Created");
			}
			else
			{
				System.out.println("File already exists in the Directory");
			}
			//Writing in a File 
					
			FileWriter FW=new FileWriter(F);
			FW.write(S);
			FW.close();
			
			//Reading from a File
			
			System.out.println("READING...");
			FileReader FR=new FileReader(F);
			int i;
			while ((i=FR.read()) != -1){
				System.out.print((char) i);
			}
			System.out.println();
				
				
		}
		catch(IOException e)
		{
			e.getMessage();
		}

	}
}

Output

Enter the Message you wish to Print in the file
Super Machines is an Online Technical Forum, founded by Mayank Singh!
File Created
READING...
Super Machines is an Online Technical Forum, founded by Mayank Singh!

In Order to delete the File:

Consider the code:

if(F.delete())
{
	System.out.println("Your File: "+F.getName()+" Has Been Deleted");
}
else
{	
	System.out.println("Could not Delete");
}

Output

Your File: SuperMachines.txt Has Been Deleted








COMMENTS