1 字节流
1.1 字节输出流 OutputStream
1)FileOutputStream:用于将数据写入到文件中。
示例:将数据写入到文件
// 1 创建存储数据的文件。
File file = new File("/Users/Mac/mydata/ioouttest.txt");
// 2 创建一个用于操作文件的字节输出流对象。一创建就必须明确数据存储目的地。
FileOutputStream fos = new FileOutputStream(file);
// 3 调用父类中的write方法。
byte[] data = "abcde".getBytes();
fos.write(data);
// 4 关闭流资源。
fos.close();
注意:
- 输出流目的是文件,会自动创建。如果文件存在,则覆盖。
示例:在文件末尾添加内容和回车换行
// 1 创建File对象
File file = new File("/Users/Mac/mydata/ioouttest.txt");
// 2 创建输出流对象
FileOutputStream fos = new FileOutputStream(file, true);
// 3 输出数据
String str = "\r\n"+"hello";
fos.write(str.getBytes());
// 4 关闭资源
fos.close();
示例:IO异常处理
File file = new File("/Users/Mac/mydata/ioouttest.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file, true);
String str = "\r\n" + "hello";
fos.write(str.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
// 判断fos是否为null,不为null时,才能关闭
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.2 字节输入流 InputStream
1)FileInputStream:从文件中读取数据
示例:使用read()方法读取数据
// 1 创建File对象
File file = new File("/Users/Mac/mydata/ioouttest.txt");
// 2 创建一个字节输入流对象,必须明确数据源,其实就是创建字节读取流和数据源相关联。
FileInputStream fis = new FileInputStream(file);
// 3 读取数据。使用 read();一次读一个字节。没有字节返回-1
int ch = 0;
while ((ch = fis.read()) != -1) {
System.out.println("ch=" + (char) ch);
}
// 4 关闭资源。
fis.close();
示例:使用read(byte[]) 方法读取数据,该方法返回读取到的字节数
// 将上面代码第三步改为如下:
// 3 创建一个字节数组。
byte[] buf = new byte[1024]; // 长度可以定义成1024的整数倍。
int len = 0;
while ((len = fis.read(buf)) != -1) {
System.out.println(new String(buf, 0, len));
}
1.3 复制文件
1)方法一
// 1 明确源和目的。
File srcFile = new File("/Users/Mac/mydata/ioouttest.txt");
File destFile = new File("/Users/Mac/mydata/ioouttest_copy.txt");
// 2 明确字节流 输入流和源相关联,输出流和目的关联。
FileInputStream fis = new FileInputStream(srcFile);
FileOutputStream fos = new FileOutputStream(destFile);
// 3 使用输入流的读取方法读取字节,并将字节写入到目的中。
int ch = 0;
while ((ch = fis.read()) != -1) {
fos.write(ch);
}
// 4 关闭资源。
fos.close();
fis.close();
2)方法二:使用缓冲数组方式复制文件
// 将上面代码第3步改成如下:
// 3 定义一个缓冲区。
byte[] buf = new byte[1024];
int len = 0;
while ((len = fis.read(buf)) != -1) {
fos.write(buf, 0, len);// 将数组中的指定长度的数据写入到输出流中。
}
2 字符流
2.1 字符输入流 Reader
1)FileReader
示例:写入读取包含中文的文件
// 读取中文
public static void readCNText() throws IOException {
// 1 创建FileReader对象
FileReader fr = new FileReader("/Users/Mac/mydata/ioouttest.txt");
// 2 读取数据
int ch = 0;
while ((ch = fr.read()) != -1) {
// 输出的字符对应的编码值
System.out.println(ch);
// 输出字符本身
System.out.println((char) ch);
}
}
// 写中文
public static void writeCNText() throws IOException {
FileOutputStream fos = new FileOutputStream("/Users/Mac/mydata/ioouttest.txt");
fos.write("学习Java".getBytes());
fos.close();
}
2.2 字符输出流 Writer
1)FileWriter
示例:将包含中文的数据写入到文件中
// 写中文
public static void writeCNText() throws IOException {
// 1 创建FileWriter对象
FileWriter fw = new FileWriter("/Users/Mac/mydata/ioouttest.txt");
// 2 写数据
fw.write("你好谢谢再见");// 这些文字都要先编码。都写入到了流的缓冲区中。
fw.flush();
fw.close();
}
注意:
- flush():刷新流的缓冲数据到文件中,刷新后该流可继续使用。
- close():先刷新,后关闭流。如果写入数据多,应一边写一边刷新。
2.3 字符流复制文件
public static void copyTextFile() throws IOException {
// 1 明确源和目的。
FileReader fr = new FileReader("/Users/Mac/mydata/ioouttest.txt");
FileWriter fw = new FileWriter("/Users/Mac/mydata/ioouttest_copy1.txt");
// 2 为了提高效率。自定义缓冲区数组。字符数组。
char[] buf = new char[1024];
int len = 0;
while ((len = fr.read(buf)) != -1) {
fw.write(buf, 0, len);
}
/* 方法二:
* 2 循环读写操作。效率低。
* int ch = 0;
* while((ch=fr.read())!=-1){
* fw.write(ch);
* }
*/
// 3 关闭资源。
fw.close();
fr.close();
}