What is Java File Handling? - Programmer's Academy

What is Java File Handling?

Java File Handling is an important concept in Java because of the various tasks carried out on documents such as read, write, etc. File Handling nevertheless, is used for creating, updating, reading and removing the document that are saved in document system. These several methods for creating, reading, updating and deleting files are common in Java. So, file handling in Java is all about reading from and writing data to a file.

 The file class is present in Java .io. package. It allows working with various forms of documents or files. To work with different format of files, and use the file class, you will need to create an object of the class and specify the file name or directory name.

For example:

//import the file class 
Import the Java .io. file

//specify the filename
File obj. new file (‘filename.txt’);

A table representing various methods used for performing operations in Java files:

 More so, Java uses the concept of Stream to make I/O operations on a file. Stream is just a sequence of data. We have the Byte Stream and the character stream. When an input is executed with a Byte data, it is called the file handling process with a Byte Stream. On the other hand, character stream is the process of input data with character.

Types of File Handlings:

There are two types of file handlings. They are FileWriter and FileReader. These files are used to carry out all the document operations in Java program.

1. FileWriter:

This file is used to create write reports that has characters. It is derived from the OutputStream class. The constructors of this class generally imagine that the Byte Buffer length and default character coding is allowed.

You have to create an OutputStreamWriter on a FileOutputStream to declare them. It is mainly used for writing continuous characters.

The following program shows how to create a text file using File Writer.

//creating a text file using fileWriter 
Import java .io. FileWriter;
Import java .io. IOException;
Class CreateFile
Public static void main (string []args) throws IOException
// Accept a string
String str = “File Handling in Java using “+
“FileWriter and FileReader”;
//attach a file to FileWriter;
FileWriter fw = new FileWriter (“output.txt”);
// read character wise from string and write
// into FileWriter
For ( int i = o; i < str. length (); i++)
fw. Write (str. charAt (i));
System out. printIn (“writing successful”);
// close the file
fw. Close ();

The following program shows how to read from text file using FileReader Java

 2. FileReader:

This is used to read a character stream data from a text document. This class emanates from the InputStreamReaderclass. In this class, the constructors are imagining that the default character coding and the default byte are correct.

For verifying the values, create InputStreamReader on a FileInputStream. Java FileReader is used for reading continuous characters. Use FileInputStream for reading continuous raw.

// Reading data from a file using FileReader 
Import java .io. FileNotFoundException;
Import java .io. FileReader
Import java .io. IOException;
Class ReadFile
Public static void. Main ( string [] args) throws IOException
// variable declaration
Int ch;
// check if file exists or not
FileReader fr = null;
fr= new FileReader (“text”);
Catch ( FileNotFoundException fe)
System . out. printIn (“FileNotFound”);
// read from FileReader till the end of the file
While (( ch= fr. Read ())! = -1)
System. Out. Print (( char) ch;
// close the file
fr. Close ();

Recommended Articles:

  1. File names and Class names in Java
  2. How is the Java platform independent?
  3. What is JVM Stack Area in Java
  4. What is Methods In Java
  5. What is a Constructor in Java?


Help Me Please: If you found any error in the content or example please let us know by comment also write your suggestion or if any doubt in the concept please feel free to write us in the comment section.

Practice your Code online here

(Visited 285 times, 1 visits today)

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
You cannot copy content of this page
Scroll Up
Would love your thoughts, please comment.x

  • Name

  • Minimum length of 8 characters.