常见IO流的使用与实践
IO流
概念梳理
字节流:是让计算机读写的,让计算机理解的内容,JAVA中的Byte等同于计算机中的字节
字符流:是让人读懂的,需要指定的编码格式将字节转为字符
编码:将字符通过指定的编码格式转为字节
解码:将字节通过指定的编码格式转为字符
Java中字节流、字符流的超类
InputStream、OutputStream、Reader、Writer
字节流用来处理二进制文件(图片、MP3、视频文件),字符流用来处理文本文件
使用案例
将外部文件读取并输出到另外一个文件
1.使用字节流
try (FileInputStream in = new FileInputStream("C:\\Users\\patry-abu\\Desktop\\临时文件\\StreamDemo.java");
FileOutputStream out = new FileOutputStream("C:\\Users\\patry-abu\\Desktop\\StreamDemo.java");) {
// datas充当的角色是一个中间桥梁
byte[] datas = new byte[1024];
int length;
while ((length = in.read(datas, 0, datas.length)) != -1) {
out.write(datas, 0, length);
}
}
以下通过直接读取datas.lenth长度文件不可取,原因是read(byte b[], int off, int len) 每次读取的字节书要求是len长度的内容,如果不是则会往前推直至到1024个字节,即会读取到就得数据信息举例:比如一个文件内容有1025个字节,每次读取len=1024个字节数,第一次能正常读,第二次发现不足1024个字节,则会将从最后一个字节往前推1024个字节,即第二次拿到的数据是从2-1025,两次读取过程中从2到1024个字节是重复的字节内容
try (FileInputStream in = new FileInputStream("C:\\Users\\patry-abu\\Desktop\\临时文件\\StreamDemo.java");
FileOutputStream out = new FileOutputStream("C:\\Users\\patry-abu\\Desktop\\StreamDemo.java");) {
// datas充当的角色是一个中间桥梁
byte[] datas = new byte[200];
while (in.read(datas, 0, datas.length) != -1) {
out.write(datas, 0, datas.length);
}
}
2.使用字符流
try (FileReader fileReader = new FileReader("原路径\\测试.txt");
FileWriter fileWriter = new FileWriter("目的路径\\测试.txt");) {
char[] datas = new char[1024];
int len;
while ((len = fileReader.read(datas, 0, datas.length)) != -1) {
fileWriter.write(datas, 0, len);
}
}
3.使用缓冲流输出文件
@Test
public void testFile() throws IOException {
try (BufferedInputStream bufferedInputStream
= new BufferedInputStream(new FileInputStream("C:\\Users\\patry-abu\\Desktop\\金字塔原理.pdf"));
BufferedOutputStream bufferedOutputStream
= new BufferedOutputStream(new FileOutputStream("C:\\Users\\patry-abu\\Desktop\\a.pdf"))) {
int length;
byte[] bytes = new byte[2 * 1024];
while ((length = bufferedInputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, length);
}
}
}
5.使用内存输出流保存字节流
@Test
public void testFile() throws IOException {
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("C:\\Users\\patry-abu\\Desktop\\b.pdf"));
try (BufferedInputStream bufferedInputStream
= new BufferedInputStream(new FileInputStream("C:\\Users\\patry-abu\\Desktop\\金字塔原理.pdf"));
// 通过内存输出流保存读取到的字节数据
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
) {
int length;
byte[] bytes = new byte[2 * 1024];
while ((length = bufferedInputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, length);
}
byte[] fileBytes = outputStream.toByteArray();
bufferedOutputStream.write(fileBytes);
// 清除内存输出流
outputStream.reset();
}
}
6.使用HttpServletResponse输出到页面json数据
- 使用字符流PrintWriter
public void write(HttpServletResponse response) {
try {
response.setContentType("application/json");
response.setCharacterEncoding("utf8");
PrintWriter writer = response.getWriter();
HashMap<String, Object> data = new HashMap<>(4);
data.put("code", 0);
data.put("meg", "成功");
Gson gson = new Gson();
writer.print(gson.toJson(data));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
- 使用字节流OutputStream
public void write(HttpServletResponse response) {
try {
response.setContentType("application/json");
response.setCharacterEncoding("utf8");
ServletOutputStream outputStream = response.getOutputStream();
HashMap<String, Object> data = new HashMap<>(4);
data.put("code", 0);
data.put("meg", "成功");
Gson gson = new Gson();
outputStream.write(gson.toJson(data).getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw new RuntimeException(e);
}
}