Java read files

Java read files

After creating and writing files in java, a user needs to know the concept of java read files. As we know to create a new file, we use createNewFile() method. The output is a Boolean value= true in case the file creation is successful. Otherwise, the program returns the output as false which means the file already exists. The system throws IOException if there is a problem in creation of the file. Similarly, to write a file, the user uses write() method. With the help of this method, the user can write() in the created file.

To read files in java, the use of scanner class comes into play. Scanner class will read the contents of the text file which exists already. In this, the scanner class breaks the input of the file into tokens. As a result, we get the output as the tokens into various types using methods such as nextLine(), hasnextLine() etc

Below is a program to illustrate how we can read files in java using scanner class:

import java.io.File;
import java.io.FileNotFoundException;  
import java.util.Scanner; 
public class DeveloperHelps {
public static void main(String[] args) {
try {
File O = new File("filename.txt");
Scanner read = new Scanner(O);
while (read.hasNextLine()) {
String data = read.nextLine();
System.out.println(data);
    }
read.close();
    } 
catch (FileNotFoundException e) {
System.out.println("There is an error while reading the file");
e.printStackTrace();
    }
  }
}  

The output of this java program using scanner class will be:

There is an error while reading the file

The constructors that can be invoked for java read class are:
FileReader(String file): This constructor opens the file in the read mode and fetches filename in the string. The system throws an exception FileNotFound in case the user cannot find the file, or if the file is missing.

FileReader(File file): This will open the file in the read mode and fetches name of the file in the file instance. The system throws an exception FileNotFound in case the user cannot find the file, or if the file is missing.

In this similar way to Scanner class, the user can also use BufferReader to read the text of the file line by line. In this method, the user can read the text from character-input stream. The user performs the buffer process again and again for better and efficient results in reading characters or arrays. The syntax for BufferReader to read a file is:

BufferReader i= new BufferReader(Reader i, int size);

Here is an example to illustrate the use of BufferReader in java:

package com.javatpoint;  
import java.io.*;  
public class DeveloperHelps{    
public static void main(String args[])throws Exception{             
    InputStreamReader x=new InputStreamReader(System.in);    
    BufferedReader br=new BufferedReader(x);            
    System.out.println("Apple is a fruit");    
    String name=br.readLine();    
    System.out.println("Cat is an animal");    
}    
} 

The output of this program will be:

Apple is a fruit
Cat is an animal

Java has launched a new version Java SE 8 in which there’s an introduction to new class for more efficient reading of file. It is java.util.stream.Stream. There is one more way of reading file content in java. We can do it using FileReader class. This class is the best one when the user wants to read character files. When the constructor of the class is invoked, it presumes that default character encoding is appropriate.   

The user can also read files in java by reading all the lines of the file in a list. The method ensures that the file will get closed when all the content is read. The bytes of the file are decoded using a particular char set.  The output is in the form of DataInputStream class.

After following the processes such as create a file, write a file, read a file, the last method which is followed is delete a file. The user can do this by using delete() method. This method deletes all the contents of the file.

Leave a comment

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