字节流
以字节为单位,进行数据的传输。
字节流的输入输出是以参照物来衡量的,Java中以程序文参照物。
OutputStream 字节输出流(写文件)
InputStream 字节输入流(读文件)
以上两个抽象类,是所有字节流的父类。
读写文件的步骤:
1.创建要绑定的文件;
2.创建输入输出流,并绑定文件;
3.读写文件;
4.关闭流资源。
File file = new File("文件路径");
// 创建文件字节输出流
FileOutputStream fos = new FileOutputStream(file);
// 写文件
fos.write(66);
fos.close();
流中的错误处理:
1.抛出异常;
2.try---catch处理
// 异常处理
File file = new File("文件路径");
// 创建一个空的引用(扩大作用域)
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
fos.write("haha".getBytes());
} catch (FileNotFoundException e) {
// 文件找不到 再向下操作无意义
// 停止程序 让程序员修改代码
throw new RuntimeException("文件找不到");
} catch (IOException e) {
// 写入失败
throw new RuntimeException("写入失败");
} finally {
// 关闭资源时 一定要确保关闭的代码可以执行)
// 非空判断(fos创建成功情况下 才关闭)
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("流关闭失败");
}
}
}
字节流又叫万能流,不但可以写文本,还可以写图片、视频、音频等。使用字节流可以对文件进行复制。
文件的复制:
File file = new File("源文件路径");
File file1 = new File("目标路径");
FileInputStream fis = null;
FileOutputStream fos =null;
int num = 0;
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(file1);
while((num = fis.read()) != -1) {
fos.write(num);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("文件未找到");
} catch (IOException e) {
throw new RuntimeException("读写失败");
} finally {
// 关闭流
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException("输入流关闭失败");
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("输出流关闭失败");
}
}
}
}
}
2.使用数组缓冲区复制
File file = new File("/Users/lanou/Desktop/test/Preview.jpg");
File file1 = new File("/Users/lanou/Desktop/test/p3.jpg");
FileInputStream fis = null;
FileOutputStream fos =null;
int len = 0;
byte[] b = new byte[1024];
try {
fis = new FileInputStream(file);
fos = new FileOutputStream(file1);
// 使用缓冲数组进行读写
while ((len = fis.read(b)) != -1) {
fos.write(b, 0, len);
}
} catch (FileNotFoundException e) {
throw new RuntimeException("文件未找到");
} catch (IOException e) {
throw new RuntimeException("读写失败");
} finally {
// 关闭流
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
throw new RuntimeException("输入流关闭失败");
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
throw new RuntimeException("输出流关闭失败");
}
}
}
}
}
文件的续写
在创建文件流对象的时候,可以使用一个构造方法,在控制续写的参数位置传入true,就可以对文件进行续写。
FileOutputStream fos = new FileOutputStream(file,true);
字符流
Writer 字符输出流(写文件)
Reader 字符输入流(读文件)
这两个类是所有字符流类的父类,也是抽象类。
FileWriter
File file = new File("资源路径");
FileWriter fw = new FileWriter(file);
// 写
fw.write(65);
// 刷新
// 相当于写入的时候有一个缓冲区
// 写的字符实际上 是先写入到缓冲区
// 刷新时 才会将写的字符全部写入到文件中
// 建议:写一次刷新一次
fw.flush();
// 利用字符数组写入
char[] c = {'x','w','t','m'};
fw.write(c);
fw.flush();
// 利用字符串写入
fw.write("张三李四");
fw.flush();
// 关闭流资源
// 关闭流资源之前 会自动刷新缓冲区
fw.close();
FileReader
// 读取文件(2种方式)
File file = new File("资源路径");
// 单个字符读
FileReader fr = new FileReader(file);
int num = 0;
while ((num = fr.read()) != -1) {
System.out.print((char)num);
}
// 利用缓冲数组读
char[] c = new char[1024];
while ((num = fr.read(c)) != -1) {
System.out.println(new String(c,0,num));
}
fr.close();
转换流
OutputStreamWriter(字符流通向字节流的桥梁)
构造方法(不传编码格式,默认使用系统的编码格式)
1.需要字节输出流
InputStreamReader(字节流通向字符流的桥梁)
构造方法
1.需要字节输入流
2.编码格式的名字(不区分大小写)
缓冲流(高效流)内部自带了一个缓冲区(字节数组)。
BufferedOutputStream(写文件)缓冲字节输出流
BufferedInputStream(读文件)缓冲字节输入流
缓冲流相较于字节流的读取更高效。