-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileContentDisplay.java
More file actions
38 lines (34 loc) · 1003 Bytes
/
Copy pathFileContentDisplay.java
File metadata and controls
38 lines (34 loc) · 1003 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
31
32
33
34
35
36
37
38
package Interns_CoreJavaAssignments_V001;
import java.io.*;
public class FileContentDisplay {
public static void main(String[] args) {
// TODO Auto-generated method stub
File file=new File("sample.txt");
BufferedReader reader= null;
try {
reader= new BufferedReader(new FileReader(file));
System.out.println("File content: ");
String line;
while ((line=reader.readLine())!=null) {
System.out.println(line);
}
}
catch(FileNotFoundException e) {
System.out.println("Enter: File not found.");
}
catch(IOException e) {
System.out.println("Error: An IO error occured.");
}
finally {
try {
if(reader!=null) {
reader.close();
}
System.out.println("File operation completed.");
}
catch(IOException e) {
System.out.println("Error: Unable to close the file.");
}
}
}
}