All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Read A File In Java Using Scanner

Last Updated : Jul 1, 2023

How To Read A File In Java Using Scanner

In this article we will show you the solution of how to read a file in java using scanner, a text file can be written and read in many different ways.

This is necessary when managing numerous applications. 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.

Java offers a number of ways to read files. Each of these approaches is suitable for reading various file types in various circumstances.

Others are better at reading shorter files, while some excel at reading longer ones.

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 of a Reader.

Because of this, it is advised to enclose any Reader whose read() operations may be expensive.

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.

The following constructors are defined in this class

When given a file to read from, FileReader(File file) creates a new FileReader. FileReader(FileDescriptor fd):

The FileDescriptor to read from FileReader(String fileName) is used to create a new FileReader: gives the name of the file to be read from when creating a new FileReader.

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.

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.

With Java 5, you may read files using the java.util.Scanner class.

Earlier, we looked at instances of reading files in Java using FileInputStream and BufferedInputStream, respectively.

A text file can be written and read in many different ways. This is necessary when managing numerous applications.

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, and parsing capabilities are offered by Scanner.

Step By Step Guide On How To Read A File In Java Using Scanner :-

import java.io.File;
import java.util.Scanner;
public class ReadFromFileUsingScanner
{
public static void main(String[] args) throws Exception
{
 File file = new File("C:\\Users\\pankaj\\Desktop\\test.txt");
 Scanner sc = new Scanner(file);
 while (sc.hasNextLine())
 System.out.println(sc.nextLine());
}
}
  1. First step, importing the Scanner class
  2. Secondly, creating a class ReadFromFileUsingScanner
  3. In the third step, we will pass the file as a parameter like File file=new File
  4. We will create a while loop
  5. Penultimate step, we will print the statement using System.out.println which will take the cursor to the next line
  6. Lastly, run the code

Conclusion :-

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

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪