Print Contents of Text File to Screen in Java

Muhammad Zeeshan Apr 30, 2022
  1. Scanner Class in Java
  2. BufferedReader Class in Java
  3. FileReader Class in Java
Print Contents of Text File to Screen in Java

This article shows ways to use Java to print the contents of a text file on the screen. In Java, there are several ways to read a text file.

It is necessary when working with a large number of applications. You may read a plain text file in Java using FileReader, BufferedReader, or Scanner.

Every utility, for example, has something special to offer. With BufferedReader, data is buffered for fast reading, while parsing is done with Scanner.

Scanner Class in Java

The Scanner parses primitive types and strings using regular expressions. A Scanner divides its input into tokens using a delimiter pattern that matches whitespace by default.

The created tokens can then be translated into other values using the below-mentioned procedures. The Scanner class is demonstrated in the example below.

To use the Scanner class, we’ve imported libraries.

import java.io.File;
import java.util.Scanner;
import java.io.File;
import java.util.Scanner;
public class Main
{
  public static void main(String[] args) throws Exception
  {
    File testfile = new File("C:\\Users\\shanii\\Desktop\\read.txt");
    Scanner scnr = new Scanner(testfile);

    while (scnr.hasNextLine())
      System.out.println(scnr.nextLine());
  }
}

BufferedReader Class in Java

This approach employs a stream of characters to read text. It buffers characters, arrays, and lines for faster reading.

The buffer size can be changed or is set to be utilized by default. For the most part, the default settings are basic.

Every read request to a Reader is usually followed by a read request to the underlying character or byte stream. As a result, as shown below, it’s a good idea to wrap a BufferedReader through any Reader whose read() operations are likely to be costly, such as FileReaders and InputStreamReaders.

BufferedReader br = new BufferedReader(Reader br, int size);

Let’s look at BufferedReader as an example to help us understand.

  1. To begin, you have to import the library java.io*.
  2. In the below example, read.txt will be the file you want to read.
import java.io.*;

public class Shani {
    public static void main(String[] args) throws Exception
    {
     File testfile = new File("C:\\Users\\shanii\\Desktop\\read.txt");
     BufferedReader br= new BufferedReader(new FileReader(testfile));
     String z;
     while ((z = br.readLine()) != null)
     System.out.println(z);
    }
}

FileReader Class in Java

This class makes it easy to read character files. The constructors of this class presume that the default character encoding and byte-buffer size are adequate.

The following are the constructors specified in this class.

  1. FileReader (File file) - creates a new FileReader from the specified File.
  2. FileReader (FileDescriptor fdt) - Given the FileDescriptor to read from, creates a new FileReader.
  3. FileReader (String fileName) - creates a new FileReader with the specified file name.

Let’s look at FileReader as an example to help us understand.

import java.io.*;

public class Shani {

    // Main driver method
    public static void main(String[] args) throws Exception
    {
     FileReader frdr = new FileReader("C:\\Users\\shanii\\Desktop\\read.txt");
     int z;
     while ((z = frdr.read()) != -1)
     System.out.print((char)z);
    }
}
Muhammad Zeeshan avatar Muhammad Zeeshan avatar

I have been working as a Flutter app developer for a year now. Firebase and SQLite have been crucial in the development of my android apps. I have experience with C#, Windows Form Based C#, C, Java, PHP on WampServer, and HTML/CSS on MYSQL, and I have authored articles on their theory and issue solving. I'm a senior in an undergraduate program for a bachelor's degree in Information Technology.

LinkedIn

Related Article - Java Print