zoukankan      html  css  js  c++  java
  • 缓冲流

    缓冲流(处理流的一种)可以提高文件传输的效率, 实际开发使用的流

    BufferedInputStream
    BufferedOutputStream   加上flush()
    BufferedReader              有一个readLine()方法
    BufferedWriter                加上flush()

    TestBuffered

    package com.aff.file;
    
    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.FileOutputStream;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import org.junit.Test;
    
    /*
     缓冲流(处理流的一种)可以提供文件传输的效率, 开发使用的流
    
     BufferedInputStream
     BufferedOutputStream           加上flush()
     BufferedReader                 有一个readLine()方法
     BufferedWriter                 加上flush()
     flush() 用于写的,最后一次可能写不满,需要刷新一下把剩下的内容写出去
     
     */
    public class TestBuffered {
        // 注意最后先关输出的流
        // 使用BufferedInputStream和BufferedOutputStream实现非文本文件的复制
        @Test
        public void testBufferedInputOutStream() {
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                // 1.提供读入,写出的文件
                String src = "C:\Users\lz\Desktop\1.avi";
                String dest = "C:\Users\lz\Desktop\2.avi";
                // 2.创建相应的节点流FileInputStream,FileOutputStream
                FileInputStream fis = new FileInputStream(src);
                FileOutputStream fos = new FileOutputStream(dest);
                // 3.将创建的节点流的对象(fis, fos)作为形参传递给缓冲流的构造器中
                bis = new BufferedInputStream(fis);
                bos = new BufferedOutputStream(fos);
                // 4.实现文件的复制
                byte[] b = new byte[1020];
                int len;
                while ((len = bis.read(b)) != -1) {
                    bos.write(b, 0, len);
                    bos.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        // 复制文件的方法
        public void copyFile(String src, String dest) {
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;
            try {
                // 1.提供读入,写出的文件
                String file1 = src;
                String file2 = dest;
                // 2.创建相应的节点流FileInputStream,FileOutputStream
                FileInputStream fis = new FileInputStream(file1);
                FileOutputStream fos = new FileOutputStream(file2);
                // 3.将创建的节点流的对象(fis, fos)作为形参传递给缓冲流的构造器中
                bis = new BufferedInputStream(fis);
                bos = new BufferedOutputStream(fos);
                // 4.实现文件的复制
                byte[] b = new byte[20];
                int len;
                while ((len = bis.read(b)) != -1) {
                    bos.write(b, 0, len);
                    bos.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 先关输出的流
                if (bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (bis != null) {
                    try {
                        bis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
        @Test
        public void testCopyFile() {
            long start = System.currentTimeMillis();
            String src = "C:\Users\lz\Desktop\1.avi";
            String dest = "C:\Users\lz\Desktop\2.avi";
            copyFile(src, dest);
            long end = System.currentTimeMillis();
            System.out.println("花费的时间:" + (end - start));// 21MB 4968--->98
    
        }
    
        // readLine()
        @Test
        public void testReadLine(){
            BufferedReader br = null;
            BufferedWriter bw = null;
             try {
    //                File file1 = new File("jdbc.properties");
    //                File file2 = new File("jdbc2.properties");
                    File file1 = new File("license1.txt");
                    File file2 = new File("license2.txt");
                    FileReader fr = new FileReader(file1);
                    FileWriter fw = new FileWriter(file2);
                    br = new BufferedReader(fr);
                    bw = new BufferedWriter(fw);
                    
                    String str;
                    while((str = br.readLine()) != null){
                              System.out.println(str);        
                              bw.write(str+"
    ");
    //                        bw.newLine();//换行
                              bw.flush();
                    }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                if(br != null){
                     try {
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
            }
                if(bw != null){
                    try {
                        bw.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
          }
      }
    }
    All that work will definitely pay off
  • 相关阅读:
    Best Time to Buy and Sell Stock I II III
    数据挖掘算法面试题
    C# 从CIL代码了解委托,匿名方法,Lambda 表达式和闭包本质
    ASP.NET MVC 5
    net破解一(反编译,反混淆-剥壳,工具推荐)
    面试题及相关参考答案
    Linux 查看内核版本命令的相关说明
    c# 获取应用程序exe文件路径及退出应用程序的几种方法
    C# WebBrowser设置代理
    c# combobox控件的使用
  • 原文地址:https://www.cnblogs.com/afangfang/p/12606443.html
Copyright © 2011-2022 走看看