欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 家装 > Java基础关键_024_IO流(二)

Java基础关键_024_IO流(二)

2025/3/19 12:16:46 来源:https://blog.csdn.net/lu_shao_feng/article/details/146324429  浏览:    关键词:Java基础关键_024_IO流(二)

目  录

一、FileReader

二、FileWriter

三、文件复制2

四、缓冲流

五、字节缓冲流

1.BufferedInputStream

2.BufferedOutputStream

3.文件复制3

六、字符缓冲流

1.BufferedReader

2.BufferedReader 常见方法

(1)mark()

(2)reset() 

3.BufferedWriter


一、FileReader

  • 文件字符输入流;
  • 是包装流;
  • 以字符形式读取文件,一次至少读取一个完整字符;
  • 创建 FileReader 对象时,若未指定字符集,则默认采取 UTF-8 解码。
public class FileReaderTest {public static void main(String[] args) {try (FileReader fileReader = new FileReader("D:\\Test.txt")) {char[] chars = new char[6];int readcount = 0;while ((readcount = fileReader.read(chars)) != -1) {System.out.print(new String(chars, 0, readcount) + "->");   // I love-> you!->}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}
}

二、FileWriter

  • 文件字符输出流;

  • 是包装流;

  • 创建 FileWriter 对象时,若未指定字符集,则默认采取 UTF-8 编码。

public class FileWriterTest {public static void main(String[] args) {try (FileWriter fileWriter = new FileWriter("D:\\Test02.txt")) {fileWriter.write("宁溘死以流亡兮,");fileWriter.write("余不忍为此态也。".toCharArray());fileWriter.write("\n");fileWriter.write("噫吁嚱鸷鸟之不群兮,", 3, 7);fileWriter.write("自前世而固然。呜呼".toCharArray(), 0, 7);} catch (IOException e) {throw new RuntimeException(e);}}
}


三、文件复制2

        使用文件字符输入输出流进行文件复制,但是只能复制普通文本文件。 

public class FileCopyTest {public static void main(String[] args) {try (FileReader fileReader = new FileReader("D:\\VueWorkspace\\cm-web\\vue.config.js");FileWriter fileWriter = new FileWriter("D:\\vue.config.js")) {char[] chars = new char[1024];int readCount = 0;while ((readCount = fileReader.read(chars)) != -1) {fileWriter.write(chars, 0, readCount);}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}
}


四、缓冲流

  1. 为提高效率,所以需要减少 IO 开销,因此产生了缓冲流;
  2. 字节缓冲流:BufferedInputStream、BufferedInputStream;
  3. 字符缓冲流:BufferedReader、BufferedWriter;
  4. FileInputStream 和 FileOutputStream 是节点流,而 BufferedInputStream 和 BufferedInputStream 是缓冲流 / 包装流 / 处理流;
  5. 缓冲流效率高,自带缓冲区,且自我维护。读写大文件时推荐使用。

五、字节缓冲流

1.BufferedInputStream

public class BufferedInputStreamTest {public static void main(String[] args) {BufferedInputStream bufferedInputStream = null;try {bufferedInputStream = new BufferedInputStream(new FileInputStream("D:\\Test.txt"));byte[] bytes = new byte[1024];int readCount = 0;while ((readCount = bufferedInputStream.read(bytes)) != -1) {System.out.println(new String(bytes, 0, readCount));    // I love you!}} catch (IOException e) {throw new RuntimeException(e);} finally {try {if (bufferedInputStream != null) {bufferedInputStream.close();    // 只需要关闭最外层包装流,不需要手动关闭节点流,因为底层会自动关闭}} catch (IOException e) {throw new RuntimeException(e);}}}
}

2.BufferedOutputStream

public class BufferedOutputStreamTest {public static void main(String[] args) {BufferedOutputStream bufferedOutputStream = null;try {bufferedOutputStream = new BufferedOutputStream(new FileOutputStream("D:\\Test02.txt"));bufferedOutputStream.write("实迷途其未远,".getBytes());bufferedOutputStream.write("\n".getBytes());bufferedOutputStream.write("觉今是而昨非。".getBytes());} catch (IOException e) {throw new RuntimeException(e);} finally {if (bufferedOutputStream != null) {try {bufferedOutputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}}
}


3.文件复制3

        使用字节缓冲流的方式复制文件,并计算总耗时。

public class FileCopyTest {public static void main(String[] args) {long begin = System.currentTimeMillis();try (BufferedInputStream in = new BufferedInputStream(new FileInputStream("D:\\Xmind\\Xmind.exe"));BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream("D:\\Xmind.exe"))) {byte[] bytes = new byte[1024];int readCount = 0;while ((readCount = in.read(bytes)) != -1) {out.write(bytes, 0, readCount);}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}long end = System.currentTimeMillis();System.out.println("使用缓冲区文件复制完成,耗时:" + (end - begin) + "毫秒");   // 使用缓冲区文件复制完成,耗时:345毫秒}
}


        对比不使用缓冲区的文件复制效率。 

public class FileCopyTest {public static void main(String[] args) {long begin = System.currentTimeMillis();try (FileInputStream in = new FileInputStream("D:\\Xmind\\Xmind.exe");FileOutputStream out = new FileOutputStream("D:\\Xmind.exe")) {byte[] bytes = new byte[1024];int readCount = 0;while ((readCount = in.read(bytes)) != -1) {out.write(bytes, 0, readCount);}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}long end = System.currentTimeMillis();System.out.println("不使用缓冲区文件复制完成,耗时:" + (end - begin) + "毫秒");   // 不使用缓冲区文件复制完成,耗时:1100毫秒}
}

         由此可见,效率差距还是比较大的。因此,建议大文件复制使用带有缓冲区的方式。


六、字符缓冲流

1.BufferedReader

public class BufferedReaderTest {public static void main(String[] args) {try (BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\Test.txt"))) {String line;while ((line = bufferedReader.readLine()) != null) {System.out.println(line);   // I love you!}} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}
}

2.BufferedReader 常见方法

(1)mark()

        在当前位置做标记。


(2)reset() 

        回到上一个标记位置。 

        两个方法共同使用,作用是实现某段内容重复读取。

public class BufferedReaderTest {public static void main(String[] args) {try (BufferedReader bufferedReader = new BufferedReader(new FileReader("D:\\Test.txt"))) {System.out.println(bufferedReader.read());  // 97System.out.println(bufferedReader.read());  // 98System.out.println(bufferedReader.read());  // 99bufferedReader.mark(2); // 设置标记位置bufferedReader.reset(); // 重置标记位置System.out.println(bufferedReader.read());  // 100System.out.println(bufferedReader.read());  // 101System.out.println(bufferedReader.read());  // 102bufferedReader.reset();System.out.println(bufferedReader.read());  // 100System.out.println(bufferedReader.read());  // 101} catch (FileNotFoundException e) {throw new RuntimeException(e);} catch (IOException e) {throw new RuntimeException(e);}}
}

3.BufferedWriter

public class BufferedWriterTest {public static void main(String[] args) {try (BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("D://Test02.txt"))) {bufferedWriter.write("人生自古谁无死,");bufferedWriter.write("\n");bufferedWriter.write("留取丹心照汗青。".toCharArray());} catch (IOException e) {throw new RuntimeException(e);}}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词