-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReadFileExample.java
More file actions
30 lines (26 loc) · 910 Bytes
/
Copy pathReadFileExample.java
File metadata and controls
30 lines (26 loc) · 910 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
27
28
29
30
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFileExample {
public static void readFile(String fileName) throws FileNotFoundException {
File file = new File(fileName);
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter file name: ");
String fileName = input.nextLine();
try {
readFile(fileName);
} catch (FileNotFoundException e) {
System.out.println("Error: File not found.");
} finally {
System.out.println("File operation attempted.");
}
input.close();
}
}