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

More examples: File Handling in Java

Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.