Java字节流与字符流读写文件

刺骨的言语ヽ痛彻心扉 2022-04-05 08:52 342阅读 0赞

字节流读写

Java中文件读写有多种方式,但有一种读写方式可以处理任何场景的文件读写,就是字节流的读写。

  1. package com.main.stream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. /**
  9. * 字节流文件读写
  10. * @author PC
  11. *
  12. */
  13. public class FileRwByByte {
  14. public static void main(String[] args) throws IOException {
  15. // 定义源文件
  16. File file = new File("D:\\springboot学习\\34116codes.zip");
  17. InputStream fis = new FileInputStream(file);
  18. // 获取文件名
  19. String fileName = file.getName();
  20. // 定义写文件路径
  21. String aimPath = "E:\\stream\\" + fileName;
  22. OutputStream fos = new FileOutputStream(aimPath);
  23. // 定义字节,接收读取到的源文件字节内容
  24. int by;
  25. while ((by = fis.read()) != -1) {
  26. fos.write(by);
  27. }
  28. fos.flush();
  29. fis.close();
  30. fos.close();
  31. }
  32. }

这里按照一个字节一个字节的读取,将读取到的字节写入到新的文件中,达到文件复制粘贴的效果。

上面程序可以正常执行,但是执行过程中,如果遇到10MB 以上的文件,可能会感觉特别的慢,原因就在于,程序中一个字节一个字节的读取,会增大文件读写的次数。

如何改进,使用字节数组,一次读取多个字节,减少文件读写次数,提交程序执行效率。

将程序中,按照字节单字节读取的部分,修改为按照字节数组读取。这时,输入流一次读取1024个字节,然后将读取到的字节直接输出写到文件中,大大增加程序的整体效率。

  1. // 定义字节数组,接收读取到的源文件字节内容
  2. byte[] bytes = new byte[1024];
  3. while (fis.read(bytes) != -1) {
  4. fos.write(bytes);
  5. }

附整段代码

  1. package com.main.stream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.OutputStream;
  8. /**
  9. * 字节流文件读写
  10. * @author PC
  11. *
  12. */
  13. public class FileRwByByte {
  14. public static void main(String[] args) throws IOException {
  15. // 定义源文件
  16. File file = new File("D:\\springboot学习\\34116codes.zip");
  17. InputStream fis = new FileInputStream(file);
  18. // 获取文件名
  19. String fileName = file.getName();
  20. // 定义写文件路径
  21. String aimPath = "E:\\stream\\" + fileName;
  22. OutputStream fos = new FileOutputStream(aimPath);
  23. // 定义字节数组,接收读取到的源文件字节内容
  24. byte[] bytes = new byte[1024];
  25. while (fis.read(bytes) != -1) {
  26. fos.write(bytes);
  27. }
  28. fos.flush();
  29. fis.close();
  30. fos.close();
  31. }
  32. }

字符流读写

上面看了字节流的读写,有单字节的读写和读取字节数组两种方式。同样,字符流的读写也有单字符读写和字符数组两种方式。

  1. package com.main.stream;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.Reader;
  7. import java.io.Writer;
  8. /**
  9. * 字符流文件读写
  10. * @author PC
  11. *
  12. */
  13. public class FileReaderDemo {
  14. public static void main(String[] args) throws IOException {
  15. // 定义源文件
  16. File file = new File("E:\\test.txt");
  17. Reader reader = new FileReader(file);
  18. // 获取文件名
  19. String fileName = file.getName();
  20. // 定义写文件路径
  21. String aimPath = "E:\\stream\\" + fileName;
  22. Writer writer = new FileWriter(aimPath);
  23. // 定义字符,接收读取到的源文件字符内容
  24. int ch;
  25. while ((ch = reader.read()) != -1) {
  26. writer.write(ch);
  27. }
  28. writer.flush();
  29. writer.close();
  30. reader.close();
  31. }
  32. }

同样的原因,单字符读写会稍微慢一点,那么换字符数组读取呢?

  1. // 定义字符数组,接收读取到的源文件字符内容
  2. char[] chars = new char[1024];
  3. while (reader.read(chars) != -1) {
  4. writer.write(chars);
  5. }

只需要将读写操作方式的按单字符读写修改为按照数组的方式即可。

附完整代码:

  1. package com.main.stream;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.Reader;
  7. import java.io.Writer;
  8. /**
  9. * 字符流文件读写
  10. * @author PC
  11. *
  12. */
  13. public class FileReaderDemo {
  14. public static void main(String[] args) throws IOException {
  15. // 定义源文件
  16. File file = new File("E:\\test.txt");
  17. Reader reader = new FileReader(file);
  18. // 获取文件名
  19. String fileName = file.getName();
  20. // 定义写文件路径
  21. String aimPath = "E:\\stream\\" + fileName;
  22. Writer writer = new FileWriter(aimPath);
  23. // 定义字符数组,接收读取到的源文件字符内容
  24. char[] chars = new char[1024];
  25. while (reader.read(chars) != -1) {
  26. writer.write(chars);
  27. }
  28. writer.flush();
  29. writer.close();
  30. reader.close();
  31. }
  32. }

纯粹防止自己忘记,文章记录。

发表评论

表情:
评论列表 (有 0 条评论,342人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Java IO——节流字符

    1、字节流与字符流 1.1 流的概念 在程序中所有的数据都是以流的方式进行传输或保存的,程序需要数据的时候要使用输入流读取数据,而当程序需要将一些数据保存起来的时候,就要使