字节流、字符流、缓冲流、转换流的基本使用
package demo02; import org.junit.Test; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; /** * @description: demo08 * @author: liuyang * @create: 2021-09-05 17:47 */ public class Demo08 { /** * 创建文件 * @throws IOException */ @Test public void test1() throws IOException { File file = new File("test.txt"); file.createNewFile(); } /** * 读取文件内容 * @throws IOException */ @Test public void test2() { FileReader fileReader = null; try { fileReader = new FileReader("test.txt"); int data = 0; while ((data = fileReader.read()) != -1) { System.out.println((char)data); } } catch (IOException e) { e.printStackTrace(); } finally { if (fileReader != null) { try { fileReader.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 对test2进行优化 */ @Test public void test3() { FileReader reader = null; try { reader = new FileReader("test.txt"); // 每次最多读取6个字符 char[] buff = new char[3]; // 每一次实际读取的字符个数 int len = 0; while ((len = reader.read(buff)) != -1) { // 得到实际读物到的字符串 String str = new String(buff, 0, len); System.out.println(str); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 字符流写入 * 默认会先将文件进行覆盖再写入 * 原文件若不存在则会自动创建 */ @Test public void test4() { FileWriter writer = null; try { writer = new FileWriter("test.txt"); writer.write("hello"); writer.write("boy"); } catch (IOException e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 原文件若不存在则会自动创建, * 第二个参数为true表示会在原文件内容末尾添加而不再是覆盖原文件了 */ @Test public void test5() { FileWriter writer = null; try { writer = new FileWriter("test.txt", true); writer.write("AAAA"); writer.write("BBB"); } catch (IOException e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 读取文件内容写入到另一个文件 * 先读完再一次写入 */ @Test public void test6() { File file = new File("test.txt"); FileReader reader = null; FileWriter writer = null; try { reader = new FileReader(file); // 每次最多读取的字符个数 char[] buff = new char[1024]; // 每次实际读取的字符个数 int len = 0; // 文件完整内容 StringBuilder sb = new StringBuilder(); while ((len = reader.read(buff)) != -1) { sb.append(new String(buff, 0, len)); } // 将读取的内容写入到text1.txt中,没有text1.txt就创建,每次会将text1.txt覆盖一次 writer = new FileWriter("text1.txt"); writer.write(sb.toString()); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (writer != null) { writer.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 读取文件内容写入到另一个文件 * 边读边写 */ @Test public void test7() { File file = new File("test.txt"); FileReader reader = null; FileWriter writer = null; try { reader = new FileReader(file); // 将读取的内容写入到text1.txt中,没有text1.txt就创建,每次会将text1.txt覆盖一次 writer = new FileWriter("text1.txt"); // 每次最多读取的字符个数 char[] buff = new char[1024]; // 每次实际读取的字符个数 int len = 0; while ((len = reader.read(buff)) != -1) { // 边读取边写入 writer.write(buff, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (writer != null) { writer.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 字节流复制图片 * 边读取边复制 */ @Test public void test8() { FileInputStream inputStream = null; FileOutputStream outputStream = null; try { inputStream = new FileInputStream("杨幂.jpg"); outputStream = new FileOutputStream("杨幂1.jpg"); // 每次读取的最大字节数 byte[] buff = new byte[1024]; // 每次实际读取的字节数 int len = 0; while ((len = inputStream.read(buff)) != -1) { // 将每次读取的字节写入到另一个文件中 outputStream.write(buff, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (inputStream != null) { inputStream.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (outputStream != null) { outputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 缓冲字节流复制文件: * 缓冲字节流比直接使用字节流的效率高,因为有缓冲区的机制。 * 资源关闭: * 先关闭外层的缓冲流再关闭内层的字节流, * 我们在关闭外层流的时候,内层流会自动被顺带关闭, * 因此我们一般只需关闭外层流。 */ @Test public void test9() { FileInputStream inputStream = null; BufferedInputStream bufferedInputStream = null; FileOutputStream outputStream = null; BufferedOutputStream bufferedOutputStream = null; try { inputStream = new FileInputStream("杨幂.jpg"); // 缓冲字节输入流 bufferedInputStream = new BufferedInputStream(inputStream); outputStream = new FileOutputStream("杨幂2.jpg"); // 缓冲字节输出流 bufferedOutputStream = new BufferedOutputStream(outputStream); // 每次最大读取的字节数 byte[] buff = new byte[1024]; // 每次实际读取到的字节数 int len = 0; while ((len = bufferedInputStream.read(buff)) != -1) { // 边读取边写出 bufferedOutputStream.write(buff, 0, len); } System.out.println("复制成功"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭资源,只需关闭外层流,内层流会顺带被自动关闭 try { if (bufferedInputStream != null) { bufferedInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (bufferedOutputStream != null) { bufferedOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 缓冲字符流复制文本文件 * 缓冲字符流比直接使用字符流的效率要高, * 因为有缓冲区的机制 */ @Test public void test10() { FileReader fileReader = null; BufferedReader bufferedReader = null; FileWriter fileWriter = null; BufferedWriter bufferedWriter = null; try { // 字符输入流 fileReader = new FileReader("test.txt"); // 缓冲字符输入流 bufferedReader = new BufferedReader(fileReader); // 字符输出流 fileWriter = new FileWriter("test2.txt"); // 缓冲字符输出流 bufferedWriter = new BufferedWriter(fileWriter); // 每次最大读取字符数 char[] buff = new char[1024]; // 每次实际读取字符数 int len = 0; while ((len = bufferedReader.read(buff)) != -1) { // 边读取边写入 bufferedWriter.write(buff, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭外层流时内层流会自动关闭对的 try { if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (bufferedWriter != null) { bufferedWriter.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 缓冲字符流复制文本文件 * 缓冲字符流比直接使用字符流的效率要高, * 因为有缓冲区的机制。 * 每次读取一行 */ @Test public void test11() { FileReader fileReader = null; BufferedReader bufferedReader = null; FileWriter fileWriter = null; BufferedWriter bufferedWriter = null; try { // 字符输入流 fileReader = new FileReader("test.txt"); // 缓冲字符输入流 bufferedReader = new BufferedReader(fileReader); // 字符输出流 fileWriter = new FileWriter("test3.txt"); // 缓冲字符输出流 bufferedWriter = new BufferedWriter(fileWriter); // 每一行读取的内容,读取到末尾返回null String lineContent = null; while ((lineContent = bufferedReader.readLine()) != null) { // 边读取边写入 bufferedWriter.write(lineContent); // 加上换行 bufferedWriter.newLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭外层流时内层流会自动关闭对的 try { if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (bufferedWriter != null) { bufferedWriter.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 转换流 * InputStreamReader实现字节输入流转换为字符输入流 */ @Test public void test12() { FileInputStream inputStream = null; InputStreamReader streamReader = null; try { // 字节输入流 inputStream = new FileInputStream("test.txt"); // 转换后的字符输入流 streamReader = new InputStreamReader(inputStream, "UTF-8"); // 每次读取最大的字符数 char[] buff = new char[2]; // 每次实际读取到的字符数 int len = 0; while ((len = streamReader.read(buff)) != -1) { // 每次读取到的字符串 String str = new String(buff, 0, len); // 打印出每次读取到的字符串 System.out.print(str); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (streamReader != null) { streamReader.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 转换流: * 将utf-8格式的文本转换为gbk格式的文本。 * InputStreamReader实现字节输入流转换为字符输入流, * OutputStreamWriter实现字符输出流转为字节输出流。 */ @Test public void test13() { FileInputStream fileInputStream = null; InputStreamReader inputStreamReader = null; FileOutputStream fileOutputStream = null; OutputStreamWriter outputStreamWriter = null; try { // 字节输入流 fileInputStream = new FileInputStream("test.txt"); // 转换后的字符输入流 inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); // 字节输出流 fileOutputStream = new FileOutputStream("text4.txt"); // 转换后的字符输出流 outputStreamWriter = new OutputStreamWriter(fileOutputStream, "GBK"); // 每次最对读取的字符数 char[] buff = new char[10]; // 每次实际读取到的字符数 int len = 0; while ((len = inputStreamReader.read(buff)) != -1) { // 边读取边写出 outputStreamWriter.write(buff, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭资源,关闭外层流时内层流会自动关闭 try { if (inputStreamReader != null) { inputStreamReader.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (outputStreamWriter != null) { outputStreamWriter.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
相识是缘