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’);

JAVA file method

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

Method Type Description
canRead() Boolean It tests whether the file is readable or not
canWrite() Boolean It tests whether the is writeable or not
createNewFile() Boolean It creates an empty file
delete() Boolean It deletes a file
exists() String It tests whether the file exists
getName () String It returns the name of the file
() getAbsolutePath String It returns the absolute pathname of the file
length() Long It returns the size the of the file in byte
list() String It returns an array of the files in the directory
mkdir() Boolean It creates a directory

 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.

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 ();
  }
}

bytes.

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

 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;
            try
           {
                    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 ();
   }
}

Leave a Reply

Your email address will not be published. Required fields are marked *