All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

How To Read Data From File In Java

Last Updated : Mar 11, 2024

How To Read Data From File In Java

In this article we will show you the solution of how to read data from file in java, there are multiple ways to read data from files in Java.

Java offers a number of methods for reading plain text files, including FileReader, BufferedReader, and Scanner. Each utility offers a unique feature, for example.

Data is buffered by BufferedReader for quick reading while parsing capabilities are offered by Scanner.

Using the BufferedReader class as method one

Using this technique, text is read from a character input stream. It uses a buffer to read lines, arrays, and characters quickly.

You can either specify the buffer size or use the default size. The default is sufficient for the majority of uses.

The underlying character or byte stream generally receives a corresponding read request for each read request made by a Reader.

Using the FileReader class as Method 2

Character file reading convenience class. The default character encoding and the default byte-buffer size are assumed to be suitable by the constructors of this class.

Using the Scanner class

a straightforward text scanner that uses regular expressions to extract primitive types and strings Using a delimiter pattern that by default matches whitespace, a Scanner tokenizes its input.

The many subsequent methods can then be used to transform the generated tokens into values of various sorts.

Using a list to read the entire file is method four.

Read every line in a file. When all bytes have been read or an I/O error or other runtime exception is thrown, this function makes sure the file is closed.

Using the chosen charset, the file's bytes are decoded into characters.

Using java.nio.file.Files, Java can read text files.

To read a file's whole contents into a byte array, we can utilise the Files class.

A method to read all lines to a list of strings is also available in the Files class. Java 7 added the Files class, which is useful if you wish to load every file's contents.

When working with small files and needing to keep the entire file's contents in memory, you should only utilise this method.

Depending on the needs of your project, you may choose to read files using a Scanner, BufferedReader, or Files.

For instance, Files and BufferedReader might be used if all you want to do is log the file.

Use the Scanner class if you want to parse the file based on a delimiter.

I want to talk about RandomAccessFile before I wrap up this tutorial. This is useful for reading text files in Java.

Step By Step Guide On How To Read Data From File In Java :-

public class GFG {
 public static void main(String[] args) throws Exception
  FileReader fr = new FileReader(
int i;
  while ((i = != -1)
  1. In the first step we will import the package*
  2. Secondly, we will create the Main class GFG
  3. In the third step, we will pass to the file as a parameter by creating FileReader.
  4. In the next step, we will declare the loop variable i.
  5. We will iterate using while loop.
  6. Lastly printing all content of a file
  7. Run the code

Conclusion :-

From the above analysis, we can conclude that there are multiple ways of reading files in Java.

Above we have done be creating a file reader class.

I hope this article on how to read data from file in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪