-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckFile.java
More file actions
27 lines (22 loc) · 753 Bytes
/
CheckFile.java
File metadata and controls
27 lines (22 loc) · 753 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import java.io.File; //import the file class
import java.io.FileNotFoundException; //import this class to handle errors
import java.util.Scanner; //import the scanner class to read text files
public class CheckFile {
public static void main(String[] args) {
try {
readFile("test1.txt");
} catch (FileNotFoundException e) {
System.out.println("Error: " + e.getMessage());
}
}
public static void readFile(String fileName) throws FileNotFoundException {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
// Read and process the contents of the file
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
scanner.close();
}
}