Using a Scanner to Read a File in Java
How to read contents of a file using Scanner class?
From Java 1.5 Scanner class was introduced. This course accepts a File, InputStream, Path and, String objects, reads all the primitive information types and Strings (from the given source) token by token using regular expressions. By default, whitespace is considered equally the delimiter (to break the data into tokens).
To read various datatypes from the source using the nextXXX() methods provided by this grade.
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 past 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 accept 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.
Instance- File Object
- Create a File object representing your required file.
- Create a Scanner grade by passing the above created file object.
- The hasNext() verifies whether the file has another line and the nextLine() method reads and returns the side by side line in the file. Using these methods read the contents of the file.
import java.io.File; import java.util.Scanner; public form 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 shop 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 side by side line in the file. Using these methods read the contents of the file.
import java.io.FileInputStream; import coffee.io.InputStream; import java.util.Scanner; public class ContentsOfFile { public static void main(Cord 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()); } Arrangement.out.println(sb); } }
Output
Hi welcome to Tutorialspoint ....
Published on 01-Aug-2019 fourteen:08:57
- Related Questions & Answers
- How to read the contents of a JSON file using Java?
- How to read a single character using Scanner class in Coffee?
- 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?
- How to read data from a file using FileInputStream?
- How to read and write a file using Javascript?
- How to delete all the file contents using PowerShell?
- How to overwrite a file to hide file contents, and make original contents unrecoverable in Linux?
- Impress contents of a file in C
- How to read information from scanner to an assortment in java?
- How to read the contents of a webpage into a string in java?
- How to read a number of characters from a text file using Python?
- Read/Write structure to a file using C
- How to read a file from command line using Python?
Source: https://www.tutorialspoint.com/how-to-read-contents-of-a-file-using-scanner-class
0 Response to "Using a Scanner to Read a File in Java"
Post a Comment