-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadFile.java
30 lines (28 loc) · 1.1 KB
/
ReadFile.java
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 ReadFile {
public static void main(String[] args) {
try {
File puppyToy = new File("puppyToy.txt");
Scanner myReader = new Scanner(puppyToy);
while (myReader.hasNextLine()) {
String data = myReader.nextLine();
System.out.println(data);
}
myReader.close();
if (puppyToy.exists()) {
System.out.println("File name: " + puppyToy.getName());
System.out.println("Absolute path: " + puppyToy.getAbsolutePath());
System.out.println("Writeable: " + puppyToy.canWrite());
System.out.println("Readable " + puppyToy.canRead());
System.out.println("File size in bytes " + puppyToy.length());
} else {
System.out.println("The file does not exist.");
}
} catch (FileNotFoundException sthWrong) {
System.out.println("error");
sthWrong.printStackTrace();
}
}
}