In this article, I will show you how to read file in Java, as well as list and explain all of the possible methods so that you can understand and learn it easily. This is perfect for beginners like you.
File handling is important and useful for all developers, and you should be familiar with it in Java. In this tutorial, let’s talk about reading a file in Java. File handling is not only for reading a file but also to create and delete a file using the Java programming language. Remember, I will only discuss reading a file in Java in this topic.
How to read file in Java
To read file in Java, we will use the Scanner class, BufferedReader class, and the FileReader class. To understand more about each method, read the descriptions below, and let’s start with the:
Scanner class
The Scanner class is available in the java.util
package and if you want to use it, you need to import it. Using the Scanner to read a file in Java is very easy, even for beginners. We can parse the file and display it in our Java program using the Scanner class and you should be aware that the Scanner class is not ideal for reading passwords because it is publicly visible. Below are the methods of the Scanner class for scanning specific words or numbers. It is also useful to read file in Java.
- Scanner (InputStream in) – This generate a scanner object from the given input stream.
- String nextLine() – This method reads the next line of input.
- String next() – This method reads the next word of input.
- int nextInt() – This method reads the input that represents an integer.
- double nextDouble() – This method reads the input that represents a floating-point number.
- boolean hasNext() – This method will identify if there is another word in the input.
- boolean hasNextInt() – This method will identify if there is another character that represents an integer.
- boolean hasNextDouble() – This method will identify if there is another character that represents an floating-point number.
Now, to read file in Java using the Scanner please follow the given example below.
Example:
import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; /** * * @author KENSOFT */ public class ReadFile { /** * @param args the command line arguments */ public static void main(String[] args) { // Reading file using Scanner try { File file = new File("C:\\Users\\KENSOFT\\Desktop\\kensoft.txt"); Scanner scan = new Scanner(file); while(scan.hasNextLine()){ System.out.println(scan.nextLine()); } } catch (FileNotFoundException e) { System.out.println(e); } } }
Output
Learn more at KENSOFTPH.COM
BufferedReader class
One of the most easiest ways to read file in Java is to use the BufferedReader class. The BufferedReader class allows you to read characters, arrays, and lines quickly and efficiently. Wrapping a BufferedReader around any reader whose read() method operations is recommended when using the BufferedReader. Consider the following example BufferedReader br = new BufferedReader(new FileReader(file));
.That’s how you make a BufferedReader object in Java. Below are the methods of the BufferedReader class for reading characters. It is also useful to read file in Java.
- close() – This method will close the stream and any system resources with it.
- lines() – This method returns a stream which are the lines read from the BufferedReader.
- mark(int readAheadLimit) – This method marks the present position in the stream.
- markSupported() – This method will identify if the stream supports the mark() method.
- read() – This method will read a single character.
- readLine() – This method will read a line of text.
- ready() – This method will identify if the stream is ready to be read.
- reset() – This method will reset the stream.
- skip(long n) – This method will skip characters
To learn how to read a file in Java using the BufferedReader class, please continue reading below.
Example
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; /** * * @author KENSOFT */ public class ReadFile { /** * @param args the command line arguments */ public static void main(String[] args) { // Reading file using BufferedReader try { File file = new File("C:\\Users\\KENSOFT\\Desktop\\kensoft.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String data; while((data = br.readLine()) != null){ System.out.println(data); } } catch (IOException e) { System.out.println(e); } } }
Output
Learn more at KENSOFTPH.COM
FileReader class
The FileReader class is one of the most basic classes in Java for reading files. It is convenient for reading character files. FileReader class is meant for reading streams of characters in Java. The constructors for the FileReader class are listed below.
Constructor Detail
These are the constructors and its descriptions in the FileReader class.
- FileReader( File file ) – This constructor generates a new FileReader to read from the specified file.
- FileReader( FileDescriptor fd ) – This constructor generate a new FileReader to read from the FileDescriptor.
- FileReader( String fileName ) – This constructor generates a new FileReader to read from the specified file name.
Please check the example below for how to read file in Java using the FileReader class.
Example
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author KENSOFT */ public class ReadFile { /** * @param args the command line arguments */ public static void main(String[] args) { // Reading file using FileReader class try { FileReader fr = new FileReader("C:\\Users\\KENSOFT\\Desktop\\kensoft.txt"); int data; while((data = fr.read()) != -1){ System.out.print((char)data); } } catch (FileNotFoundException e) { System.out.println(e); } catch (IOException ex) { Logger.getLogger(ReadFile.class.getName()).log(Level.SEVERE, null, ex); } } }
Output
Learn more at KENSOFTPH.COM
How to get file information in Java
To get the file information in Java, we will use the File method. The information we are going to get from the file are these: File name, Absolute path, Writeable, Readable, and the File size. It is also part on how to read file in Java. To do this, take a look at the example provided below.
Example
import java.io.File; /** * * @author KENSOFT */ public class ReadFile { /** * @param args the command line arguments */ public static void main(String[] args) { // Get the file information File file = new File("C:\\Users\\KENSOFT\\Desktop\\kensoft.txt"); if (file.exists()) { System.out.println("File name: " + file.getName()); System.out.println("Absolute path: " + file.getAbsolutePath()); System.out.println("Writeable: " + file.canWrite()); System.out.println("Readable " + file.canRead()); System.out.println("File size in bytes " + file.length()); } else { System.out.println("The file does not exist."); } } }
Output
File name: kensoft.txt Absolute path: C:\Users\KENSOFT\Desktop\kensoft.txt Writeable: true Readable true File size in bytes 38
To read file in Java is very easy, I hope you find this tutorial useful and helpful.