forked from Somersames/JavaBasis
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyPath.java
More file actions
31 lines (29 loc) · 1.03 KB
/
MyPath.java
File metadata and controls
31 lines (29 loc) · 1.03 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
package nio2;
import java.io.*;
import java.nio.file.Path;
import java.nio.file.Paths;
/**
* Created by szh on 2017/5/6.
*/
public class MyPath {
private void pa() throws IOException {
Path path = Paths.get("D:\\JavaBasis\\新建文本文档.txt");
File file =path.toFile(); //相当于调用new File(this.fileName);
if(file.exists()){
System.out.println("Y");
}
// FileReader fileReader = (FileReader) new InputStreamReader(new FileInputStream(file)); 这样转会出错,因为Filereader和InputStream都属于Reader的子类
Reader fileReader = new InputStreamReader(new FileInputStream(file));
char[] c =new char[1024];
int len =0;
StringBuffer sb =new StringBuffer();
if((len=fileReader.read(c)) != -1){
// String s=new String(c,0,len);
sb.append(new String(c,0,len));
}
System.out.println(sb.toString());
}
public static void main(String args[]) throws IOException {
new MyPath().pa();
}
}