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:


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);
			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");
				System.out.println("File Created");
				System.out.println("File already exists in the Directory");
			//Writing in a File 
			FileWriter FW=new FileWriter(F);
			//Reading from a File
			FileReader FR=new FileReader(F);
			int i;
			while ((i=FR.read()) != -1){
				System.out.print((char) i);
		catch(IOException e)



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

In Order to delete the File:

Consider the code:

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


Your File: SuperMachines.txt Has Been Deleted

Was this page helpful? YES NO

Are you a blogger? Join our Blogging forum.

Comments and Discussions

We are using Google to publish ads on our website; Google has its own privacy policies. They may save log, cookies on your system. Google may also collect information of your system like IP address, region, city, country. For more details please go through the Google’s privacy policy.