IO流操作文件

  • 创建源 File

  • 选择创建流 InputStream OutputStream Reader Writer

  • 读写操作 write() read()

  • 关闭资源 close()

FileReader

FileReader类从InputStreamReader类继承而来。该类按字符读取流中数据。可以通过以下几种构造方法创建需要的对象。(输入流 节点流 字符流 )

在给定从中读取数据的 File 的情况下创建一个新 FileReader。

1
public FileReader(File file)

在给定从中读取数据的 FileDescriptor 的情况下创建一个新 FileReader。

1
public FileReader(FileDescriptor fd)

在给定从中读取数据的文件名的情况下创建一个新 FileReader。查看源码得知,当fileName不为空时,会调用new file(fileName)创建出一个file对象,从而简化代码。

1
public FileReader(String fileName) 

)

将的hello.txt文件内容读入程序中,并输出到控制台

标准写法

FileWriter

FileWriter 类从 OutputStreamWriter 类继承而来。该类按字符向流中写入数据。可以通过以下几种构造方法创建需要的对象。(输出流 字符流 节点流)

在给出 File 对象的情况下构造一个 FileWriter 对象。

1
public FileWriter(File file)

在给出 File 对象的情况下构造一个 FileWriter 对象。

1
public FileWriter(File file, boolean append)

构造与某个文件描述符相关联的 FileWriter 对象。

1
public FileWriter(FileDescriptor fd)

在给出文件名的情况下构造 FileWriter 对象,它具有指示是否挂起写入数据的 boolean 值。

1
public FileWriter(String fileName, boolean append)

File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。

append:如果 append 参数为 true,则将字节写入文件末尾处,从原有文件中继续写入,相当于追加信息。如果 append 参数为 false, 则写入文件开始处即对原有文件的覆盖。默认为false

FileReader和FileWriter的共同使用

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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import org.junit.Test;

import java.io.*;

/**
* 一、流的分类:
* 1.操作数据单位:字节流、字符流
* 2.数据的流向:输入流、输出流
* 3.流的角色:节点流、处理流
* <p>
* 二、流的体系结构
* 抽象基类 节点流(或文件流) 缓冲流(处理流的一种)
* InputStream FileInputStream (read(byte[] buffer)) BufferedInputStream (read(byte[] buffer))
* OutputStream FileOutputStream (write(byte[] buffer,0,len) BufferedOutputStream (write(byte[] buffer,0,len) / flush()
* Reader FileReader (read(char[] cbuf)) BufferedReader (read(char[] cbuf) / readLine())
* Writer FileWriter (write(char[] cbuf,0,len) BufferedWriter (write(char[] cbuf,0,len) / flush()
*
*/
public class FileReaderWriterTest {

public static void main(String[] args) {
File file = new File("hello.txt");//相较于当前工程
System.out.println(file.getAbsolutePath());

File file1 = new File("day09\\hello.txt");
System.out.println(file1.getAbsolutePath());
}

/**
* 将的hello.txt文件内容读入程序中,并输出到控制台
* <p>
* 说明点:
* 1. read()的理解:返回读入的一个字符。如果达到文件末尾,返回-1
* 2. 异常的处理:为了保证流资源一定可以执行关闭操作。需要使用try-catch-finally处理
* 3. 读入的文件一定要存在,否则就会报FileNotFoundException。
*/
@Test
public void testFileReader() {
FileReader fr = null;
try {
//1.实例化File类的对象,指明要操作的文件
File file = new File("hello.txt");
//2.提供具体的流
fr = new FileReader(file);

//3.数据的读入
//read():返回读入的一个字符。如果到达文件末尾,返回-1
//方式一:
/* int data = fr.read();
while (data != -1) {
System.out.print((char) data);
data = fr.read();
}*/

//方式二:语法上针对方式一的修改
int data;
while ((data = fr.read()) != -1) {
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流的关闭连接
try {
if (fr != null) {
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

//对read()操作升级:使用read的重载方法
@Test
public void testFileReader1() {
FileReader fr = null;
try {
//1.File类的实例化
File file = new File("hello.txt");

//2.FileReader流的实例化
fr = new FileReader(file);

//3.读入的操作
//read(char[] cbuf):返回每次读入cbuf数组中的字符的个数。如果达到文件末尾,返回-1
char[] cbuf = new char[5];
int len;
while ((len = fr.read(cbuf)) != -1) {
//方式一:
//错误的写法
// for(int i = 0;i < cbuf.length;i++){
// System.out.print(cbuf[i]);
// }
//正确的写法
// for(int i = 0;i < len;i++){
// System.out.print(cbuf[i]);
// }
//方式二:
//错误的写法,对应着方式一的错误的写法
// String str = new String(cbuf);
// System.out.print(str);
//正确的写法
String str = new String(cbuf, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fr != null) {
//4.资源的关闭
try {
fr.close();
} catch (IOException e) {
e.printStackTrace();
}

}
}

}

/**
* 从内存中写出数据到硬盘的文件里。
* <p>
* 说明:
* 1. 输出操作,对应的File可以不存在的。并不会报异常
* 2.
* File对应的硬盘中的文件如果不存在,在输出的过程中,会自动创建此文件。
* File对应的硬盘中的文件如果存在:
* 如果流使用的构造器是:FileWriter(file,false) / FileWriter(file):对原有文件的覆盖
* 如果流使用的构造器是:FileWriter(file,true):不会对原有文件覆盖,而是在原有文件基础上追加内容
*/
@Test
public void testFileWriter() {
FileWriter fw = null;

try {
//1.提供File类的对象,指明写出到的文件
File file = new File("hello1.txt");

//2.提供FileWriter的对象,用于数据的写出
fw = new FileWriter(file, true);

//3.写出的操作
fw.write("I have a dream!\n");
fw.write("you need to have a dream!\n");
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.流资源的关闭
if (fw != null) {

try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

@Test
public void testFileReaderFileWriter() {
FileReader fr = null;
FileWriter fw = null;
try {
//1.创建File类的对象,指明读入和写出的文件
File srcFile = new File("hello.txt");
File destFile = new File("hello2.txt");

//不能使用字符流来处理图片等字节数据
// File srcFile = new File("爱情与友情.jpg");
// File destFile = new File("爱情与友情1.jpg");

//2.创建输入流和输出流的对象
fr = new FileReader(srcFile);
fw = new FileWriter(destFile);

//3.数据的读入和写出操作
char[] cbuf = new char[5];
int len;//记录每次读入到cbuf数组中的字符的个数
while ((len = fr.read(cbuf)) != -1) {
//每次写出len个字符
fw.write(cbuf, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
//4.关闭流资源
try {
if (fw != null)
fw.close();
} catch (IOException e) {
e.printStackTrace();
}

try {
if (fr != null)
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}