-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRead_file.java
More file actions
27 lines (24 loc) · 732 Bytes
/
Read_file.java
File metadata and controls
27 lines (24 loc) · 732 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
// Import the File class
import java.io.File;
// Import this class for handling errors
import java.io.FileNotFoundException;
// Import the Scanner class to read content from text files
import java.util.Scanner;
public class Read_file {
public static void main(String[] args)
{
try {
File Obj = new File("myfile.txt");
Scanner Reader = new Scanner(Obj);
while (Reader.hasNextLine()) {
String data = Reader.nextLine();
System.out.println(data);
}
Reader.close();
}
catch (FileNotFoundException e) {
System.out.println("An error has occurred.");
e.printStackTrace();
}
}
}