forked from vagabond1-1983/JavaRock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileReaderWriterDemo.java
63 lines (60 loc) · 1.89 KB
/
FileReaderWriterDemo.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.test.basic.chapter7;
import java.io.*;
/**
* Created by beigui on 2016/2/20.
* 功能:文件内容的写入 -- FileWriter
* 文件内容的读取 -- FileReader
*/
public class FileReaderWriterDemo {
public static void main(String[] args) {
File file = new File("d:/test", "work.txt");
FileWriter writer = null;
String content = "Java 学习 File Writer";
//文件不存在则创建
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
//文件写入逻辑
try {
writer = new FileWriter(file);
writer.write(content);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭流的操作在finally代码段中做,为了即使出异常,打开的流也会被关闭
if (null != writer) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//文件读取操作
FileReader in = null;
try {
char[] contentReader = new char[1024];
in = new FileReader(file);
int length = in.read(contentReader);
System.out.println("文件中的信息是:" + new String(contentReader, 0, length));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}