Reading file alongside Scanner
From Java v onwards java.util.Scanner flat tin last used to read file inward Java. Earlier nosotros accept seen instance of reading file inward Java using FileInputStream too reading file work past times work using BufferedInputStream too inward this Java tutorial nosotros volition See How tin nosotros purpose Scanner to read files inward Java. Scanner is a utility flat inward java.util bundle too provides several convenient method to read int, long, String, double etc from rootage which tin last an InputStream, a file or a String itself. As noted on How to instruct input from User, Scanner is besides an tardily means to read user input using System.in (InputStream) every bit source.Main payoff of using Scanner for reading file is that it allows you lot to alter delimiter using useDelimiter() method, So you lot tin purpose whatever other delimiter similar comma, pipage instead of white space.
How to Read File inward Java - Scanner Example

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* Java programme to read file using Scanner flat inward Java.
* java.util.Scanner is added on Java v too offering convenient method to read data
*
* @author
*/
public class ScannerExample {
public static void main(String args[]) throws FileNotFoundException {
//creating File event to reference text file inward Java
File text = new File("C:/temp/test.txt");
//Creating Scanner instnace to read File inward Java
Scanner scnr = new Scanner(text);
//Reading each work of file using Scanner class
int lineNumber = 1;
while(scnr.hasNextLine()){
String work = scnr.nextLine();
System.out.println("line " + lineNumber + " :" + line);
lineNumber++;
}
}
}
Output:
work 1 :--------------------- START-----------------------------------------------------
work 2 :Java provides several means to read files.
line 3 :You tin read file using Scanner, FileReader, FileInputStream too BufferedReader.
line 4 :This Java programme shows How to read file using java.util.Scanner class.
line 5 :--------------------- END--------------------------------------------------------
import java.io.FileNotFoundException;
import java.util.Scanner;
/**
*
* Java programme to read file using Scanner flat inward Java.
* java.util.Scanner is added on Java v too offering convenient method to read data
*
* @author
*/
public class ScannerExample {
public static void main(String args[]) throws FileNotFoundException {
//creating File event to reference text file inward Java
File text = new File("C:/temp/test.txt");
//Creating Scanner instnace to read File inward Java
Scanner scnr = new Scanner(text);
//Reading each work of file using Scanner class
int lineNumber = 1;
while(scnr.hasNextLine()){
String work = scnr.nextLine();
System.out.println("line " + lineNumber + " :" + line);
lineNumber++;
}
}
}
Output:
work 1 :--------------------- START-----------------------------------------------------
work 2 :Java provides several means to read files.
line 3 :You tin read file using Scanner, FileReader, FileInputStream too BufferedReader.
line 4 :This Java programme shows How to read file using java.util.Scanner class.
line 5 :--------------------- END--------------------------------------------------------
This is the content of test.txt file exception work numbers. You encounter it doesn't require much coding to read file inward Java using Scanner. You simply postulate to exercise an event of Scanner too you lot are cook to read file.
Further Learning
Complete Java Masterclass
What is retentiveness mapped file inward Java
No comments:
Post a Comment