forked from yfain/Java4Kids_code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyTextFileReader.java
More file actions
44 lines (27 loc) · 1.05 KB
/
Copy pathMyTextFileReader.java
File metadata and controls
44 lines (27 loc) · 1.05 KB
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
39
40
41
42
43
44
package niosamples;
import java.io.BufferedReader;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class MyTextFileReader {
public static void main(String[] args){
Path path = Paths.get("abc.dat");
System.out.println("The absolute path is " + path.toAbsolutePath());
try {
if ( Files.exists(path)){
System.out.println("The file size is " + Files.size(path));
}
BufferedReader bufferedReader= Files.newBufferedReader(path, StandardCharsets.UTF_8);
String currentLine;
while ((currentLine = bufferedReader.readLine()) != null){
System.out.println(currentLine);
}
} catch (IOException ioe) {
System.out.println("Can't read file: " + ioe.getMessage());
}
System.out.println("Your default character encoding is " + Charset.defaultCharset());
}
}