zoukankan      html  css  js  c++  java
  • 处理流——缓冲流

      抽象基类 节点流(文件流) 缓冲流
    InputStream(字节流) FileInputStream BufferedInputStream
    OutputStream(字节流) FileOutputStream BufferedOutputStream
      Reader(字符流) FileReader BufferedReader
      Writer(字符流) FileWriter BufferedWriter
      1 @Test
      2 
      3     public void testBufferedReader() throws Exception {
      4 
      5         File f1 = new File("hello3.txt");
      6 
      7         FileReader fr = new FileReader(f1);
      8 
      9         File f2 = new File("hello5.txt");
     10 
     11         FileWriter fw = new FileWriter(f2);
     12 
     13         BufferedReader br = new BufferedReader(fr);
     14 
     15         BufferedWriter bw = new BufferedWriter(fw);
     16 
     17 // int len;
     18 
     19 // char[] c = new char[100];
     20 
     21 // while((len=br.read(c)) != -1){
     22 
     23 // String str = new String(c,o,len);
     24 
     25 // System.out.println(str);
     26 
     27 // }
     28 
     29         String str = "";
     30 
     31         while ((str = br.readLine()) != null) {
     32 
     33             bw.write(str);
     34 
     35             bw.newLine();
     36 
     37             bw.flush();
     38 
     39         }
     40 
     41         bw.close();
     42 
     43         br.close();
     44 
     45         fw.close();
     46 
     47         fr.close();
     48 
     49     }
     50 
     51 //使用缓冲流的拷贝文件封装的方法
     52 
     53 // @Test
     54 
     55 // public void copyFile(String src,String desc) throws Exception{
     56 
     57 // long ct1= System.currentTimeMillis();
     58 
     59 // File file = new File(src);
     60 
     61 // File file2 = new File(desc);
     62 
     63 // FileInputStream fis = new FileInputStream(file);
     64 
     65 // FileOutputStream fos = new FileOutputStream(file2);
     66 
     67 // BufferedInputStream bis = new BufferedInputStream(fis);
     68 
     69 // BufferedOutputStream bos = new BufferedOutputStream(fos);
     70 
     71 // int len;
     72 
     73 // byte[] b = new byte[1024];
     74 
     75 // while((len = (bis.read(b))) != -1){
     76 
     77 // bos.write(b, 0, len);
     78 
     79 // }
     80 
     81 // long ct2 = System.currentTimeMillis();
     82 
     83 // long time = ct2-ct1;
     84 
     85 // System.out.println(time);
     86 
     87 // bos.close();
     88 
     89 // bis.close();
     90 
     91 // fos.close();
     92 
     93 // fis.close();
     94 
     95 // }
     96 
     97 //使用BufferedInputOutputStream实现非文本文件的复制
     98 
     99     @Test
    100 
    101     public void BufferedInputOutputStream() throws Exception {
    102 
    103         long ct1 = System.currentTimeMillis();
    104 
    105         File file = new File("/Users/lixiuming/Desktop/商品详情图片/detail-1.jpg");
    106 
    107         File file2 = new File("/Users/lixiuming/Desktop/project/day_15/detail-3.jpg");
    108 
    109         FileInputStream fis = new FileInputStream(file);
    110 
    111         FileOutputStream fos = new FileOutputStream(file2);
    112 
    113         BufferedInputStream bis = new BufferedInputStream(fis);
    114 
    115         BufferedOutputStream bos = new BufferedOutputStream(fos);
    116 
    117         int len;
    118 
    119         byte[] b = new byte[1024];
    120 
    121         while ((len = (bis.read(b))) != -1) {
    122 
    123             bos.write(b, 0, len);
    124 
    125             bos.flush();// 缓冲流独有的东西
    126 
    127         }
    128 
    129         long ct2 = System.currentTimeMillis();
    130 
    131         long time = ct2 - ct1;
    132 
    133         System.out.println(time);
    134 
    135         bos.close();
    136 
    137         bis.close();
    138 
    139         fos.close();
    140 
    141         fis.close();
    142 
    143     }

    缓冲流属于处理流,包在节节点流外面

  • 相关阅读:
    redis持久化RDB和AOF
    线程同步的几种方法
    JRE和JDK的区别
    Spring-两种配置容器
    为什么String类是不可变的?
    Oracle 每五千条执行一次的sql语句
    Executor , ExecutorService 和 Executors
    常见框架单例、多例与线程安全性总结
    mysql 的S 锁和X锁的区别
    linux下使用shell脚本自动化部署项目
  • 原文地址:https://www.cnblogs.com/lixiuming521125/p/6428670.html
Copyright © 2011-2022 走看看