Skip to content

Commit ab723d3

Browse files
authored
Program to read files using Java Scanner
1 parent a935632 commit ab723d3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

FileScannerPrinter

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import java.io.File;
2+
import java.io.FileNotFoundException;
3+
import java.util.Scanner;
4+
5+
public class FileScannerPrinter {
6+
7+
public static void main(String[] args) throws FileNotFoundException {
8+
// TODO Auto-generated method stub
9+
10+
//creating File instance to reference text file in Java
11+
File text = new File("test.txt");
12+
13+
//Creating Scanner instnace to read File in Java
14+
Scanner scnr = new Scanner(text);
15+
16+
//Reading each line of file using Scanner class
17+
int lineNumber = 1;
18+
19+
while(scnr.hasNextLine()){
20+
String lineContent = scnr.nextLine();
21+
System.out.println(lineNumber + " " + lineContent);
22+
lineNumber++;
23+
}
24+
25+
}
26+
27+
}

0 commit comments

Comments
 (0)