字节输入输出流的应用

布满荆棘的人生 2023-08-17 16:09 160阅读 0赞

1、文件复制

用FileInputStream和FileOutPutStream实现文件的复制,此方法不能复制文件夹。

  1. package pers.zhh.copy;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. public class CopyDemo {
  6. public static void main(String[] args) throws IOException {
  7. FileInputStream fi = new FileInputStream("M:\\数据库.zip");
  8. FileOutputStream fo = new FileOutputStream("M:\\数据库1.zip");
  9. int num = 0;
  10. long startTime = System.currentTimeMillis();
  11. while ((num = fi.read()) != -1) {
  12. fo.write(num);
  13. }
  14. fo.close();
  15. fi.close();
  16. long endTime = System.currentTimeMillis();
  17. System.out.println("执行此程序用了" + (endTime - startTime) + "毫秒。");
  18. }
  19. }

1392562-20190816202354544-732619518.png

(2)缓冲数组:

  1. package pers.zzz.copy;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. public class CopyDemo1 {
  6. public static void main(String[] args) throws IOException {
  7. FileInputStream fi = new FileInputStream("M:\\网页设计.zip");
  8. FileOutputStream fo = new FileOutputStream("M:\\网页设计3.zip");
  9. byte[] buf = new byte[1024];
  10. int len = 0;
  11. long startTime = System.currentTimeMillis();
  12. while ((len = fi.read(buf)) != -1) {
  13. fo.write(buf, 0, len); // 将数组中的指定长度的数据写入到输出流中。
  14. }
  15. fo.close();
  16. fi.close();
  17. long endTime = System.currentTimeMillis();
  18. System.out.println("执行此程序用了" + (endTime - startTime) + "毫秒。");
  19. }
  20. }

1392562-20190816203329623-735988080.png

在第一个方法中,一次只能读取一个数据字节,复制只有几M的数据花了7s时间,效率极低。而第二种采用缓冲数组的方式,复制接近1G的文件只花费了4s的时间,效率大大提升。

2、IO流的异常处理

  1. package pers.zzz.copy;
  2. import java.io.FileInputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. public class CopyDemo1 {
  6. public static void main(String[] args) {
  7. FileOutputStream fo = null;
  8. FileInputStream fi = null;
  9. long startTime = System.currentTimeMillis();
  10. try {
  11. fi = new FileInputStream("M:\\网页设1计.zip");
  12. fo = new FileOutputStream("M:\\网页设计11.zip");
  13. byte[] buf = new byte[1024];
  14. int len = 0;
  15. while ((len = fi.read(buf)) != -1) {
  16. fo.write(buf, 0, len); // 将数组中的指定长度的数据写入到输出流中。
  17. }
  18. } catch (IOException e) {
  19. System.out.println(e.toString());
  20. } finally {
  21. if (fo != null) {
  22. try {
  23. fo.close();
  24. } catch (IOException e) {
  25. System.out.println(e.toString());
  26. throw new RuntimeException();
  27. }
  28. }
  29. if (fi != null) {
  30. try {
  31. fi.close();
  32. } catch (IOException e) {
  33. System.out.println(e.toString());
  34. throw new RuntimeException();
  35. }
  36. }
  37. }
  38. long endTime = System.currentTimeMillis();
  39. System.out.println("执行此程序用了" + (endTime - startTime) + "毫秒。");
  40. }
  41. }

1392562-20190816211231687-990594445.png

转载于:https://www.cnblogs.com/zhai1997/p/11366386.html

发表评论

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

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

相关阅读