- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to read contents of a file using Scanner class?
From Java 1.5 Scanner class was introduced. This class accepts a File, InputStream, Path and, String objects, reads all the primitive data types and Strings (from the given source) token by token using regular expressions. By default, whitespace is considered as the delimiter (to break the data into tokens).
To read various datatypes from the source using the nextXXX() methods provided by this class.
Reading the contents of a file −
To read the contents of a file, Scanner class provides various constructors.
Sr.No | Constructors and Description |
---|---|
1 | Scanner(File source) Used to read data from the file represented by the given File object. |
2 | Scanner(InputStream source) Used to read data from the specified InputStream. |
3 | Scanner(Path source) Used to read data from the file represented by the specified file object. |
Assume we have a file myFile.txt in the path D:\ as shown below −
Following examples demonstrate how to read data from this file using the above constructors.
Example- File Object
- Create a File object representing your required file.
- Create a Scanner class by passing the above created file object.
- The hasNext() verifies whether the file has another line and the nextLine() method reads and returns the next line in the file. Using these methods read the contents of the file.
import java.io.File; import java.util.Scanner; public class ContentsOfFile { public static void main(String args[]) throws Exception { //Creating the File object File file = new File("D:\MyFile.txt"); //Creating a Scanner object Scanner sc = new Scanner(file); //StringBuffer to store the contents StringBuffer sb = new StringBuffer(); //Appending each line to the buffer while(sc.hasNext()) { sb.append(" "+sc.nextLine()); } System.out.println(sb); } }
Output
Hi welcome to Tutorialspoint ....
Example- InputStream Object
- Create an FileInputStream object by passing the path of the required file to its constructor, as a parameter.
- Create a Scanner class by passing the above created FileInputStream object.
- The hasNext() verifies whether the file has another line and the nextLine() method reads and returns the next line in the file. Using these methods read the contents of the file.
import java.io.FileInputStream; import java.io.InputStream; import java.util.Scanner; public class ContentsOfFile { public static void main(String args[]) throws Exception { //Creating the File object InputStream inputStream = new FileInputStream("D:\MyFile.txt"); //Creating a Scanner object Scanner sc = new Scanner(inputStream); //StringBuffer to store the contents StringBuffer sb = new StringBuffer(); //Appending each line to the buffer while(sc.hasNext()) { sb.append(" "+sc.nextLine()); } System.out.println(sb); } }
Output
Hi welcome to Tutorialspoint ....
- Related Articles
- How to read a single character using Scanner class in Java?
- How to read the contents of a JSON file using Java?
- Golang Program to Read the Contents of a File
- C# Program to read contents of a file into a string at once
- How to store the contents of arrays in a file using Java?
- C# Program to Read and Write a Byte Array to File using FileStream Class
- How to delete all the file contents using PowerShell?
- How to read data from a file using FileInputStream?
- How to read and write a file using Javascript?
- How to overwrite a file to hide file contents, and make original contents unrecoverable in Linux?
- How to read data from scanner to an array in java?
- How to read a file from command line using Python?
- How to read the contents of a web page without using any external library in Java?
- How to read a number of characters from a text file using Python?
- Read/Write Class Objects from/to File in C++

Advertisements