Javatpoint Logo
Javatpoint Logo

Different Ways of Reading a Text File in Java

In Java, there are various methods for creating and accessing text files. It is necessary to do this when working with numerous apps. Java has multiple methods for reading plain text files, such as FileReader, BufferedReader, and Scanner. Each utility offers a unique feature; for example, BufferedReader buffers data for quick reading, whereas Scanner has parsing capabilities.

There are different approaches for reading of text file in java. They are as follows.

  1. BufferedReader class for Reading text file
  2. FileReader class for Reading text file
  3. Scanner class for reading text file
  4. Reading the whole file in a List
  5. Read a text file as String

In Java, we can also read a text file line by line by using Scanner and BufferReader. Next, java.util.stream.Stream, a new Stream class introduced in Java SE 8, offers a more effective and slow method of reading a file.

Approach: BufferedReader class for Reading text file

Text is read using this method from a character input stream. It does have a buffer to make reading characters, arrays, and lines more efficient. One can either utilise the default size of the buffer or specify the size. For most uses, the default value is sufficient. Typically, every read request issued to a reader prompts an equivalent read request to be issued to the character or byte stream underneath it.

Therefore, as seen below, it is recommended to wrap any Reader, including FileReaders and InputStreamReaders, whose read() operations may be expensive, in a BufferedReader:

Syntax:

Implementation:

FileName: ReadingTextFile.java

Output:

Hello World
Welcome to the house
Have a wonderful day

Approach: FileReader class for Reading text file

A realistic course on reading character files. The constructors of this class operate under the assumption that the default byte-buffer size and character encoding are suitable.

The following constructors are defined in this class:

  1. FileReader(File file): Provide the file to read from when creating a new FileReader.
  2. FileReader(FileDescriptor fd): Provide the FileDescriptor to read from when creating a new FileReader.
  3. FileReader(String fileName): Gives the name of the file to be read from when creating a new FileReader.

Implementation:

FileName: ReadingTextFile1.java

Output:

Hello World
Welcome to the house
Have a wonderful day

Approach: Scanner class for reading text file

A straightforward text scanner that uses regular expressions to interpret strings and primitive types. By matching whitespace as the default delimiter pattern, a scanner divides its input into tokens. The many subsequent methods can then be used to transform the generated tokens into values of various sorts.

Case 1: With Using loops

Implementation:

FileName: ReadingTextFile2.java

Output:

Hello World
Welcome to the house
Have a wonderful day

Case 2: Without Using loops

Implementation:

FileName: ReadingTextFile3.java

Output:

Hello World
Welcome to the house
Have a wonderful day

Approach: Reading the whole file in a List

Open a file and read every line. By using this method, you may be guaranteed that the file will close either when all the bytes have been read or if a runtime exception or I/O fault is raised. The chosen charset is used to decode the file's bytes into characters.

Syntax:

Note:

The following are recognised as line terminators by the above method:

Implementation:

FileName: ReadingTextFile4.java

Output:

Hello World
Welcome to the house
Have a wonderful day

Approach: Read a text file as String

Java's File Class's readString() function is used to read content from the provided file.

Syntax:

Return Value: The file's content is returned by this method in String format.

Note: Since Java 11 was released, files have been read into String using the File.readString() method.

Implementation:

FileName: ReadingTextFile5.java

Output:

Hello World
Welcome to the house
Have a wonderful day






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA