美文网首页Java学习笔记
java 学习笔记之 IO流笔记

java 学习笔记之 IO流笔记

作者: 红姑娘 | 来源:发表于2016-11-17 16:03 被阅读13次

    IO流的图解如下:

    IO流图解副本.jpg

    测试对比

    /*
     * 数据源
     *      神话.mp4
     * 目的地
     *      sh.mp4
     * 
     * 基本字节流一次读取一个字节:共耗时61298毫秒
     * 基本字节流一次读取一个字节数组:共耗时83毫秒
     * 高效字节流一次读取一个字节数组:共耗时651毫秒
     * 高效字节流一次读取一个字节数组:共耗时32毫秒
     */
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        
        //1.基本字节流一次读取一个字节
        // method1("神话.mp4", "sh.mp4");
        
        //2.基本字节流一次读取一个字节数组
        // method2("神话.mp4", "sh.mp4");
        
        //3.高效字节流一次读取一个字节
        // method3("神话.mp4", "sh.mp4");
        
        //4.高效字节流一次读取一个字节数组
        method4("神话.mp4", "sh.mp4");
        long end = System.currentTimeMillis();
        System.out.println("共耗时" + (end - start) + "毫秒");
    }
    
    //4.高效字节流一次读取一个字节数组
    private static void method4(String srcString, String destString)
            throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                srcString));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destString));
    
        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = bis.read(bys)) != -1) {
            bos.write(bys, 0, len);
        }
    
        bos.close();
        bis.close();
    }
    
    //3.高效字节流一次读取一个字节
    private static void method3(String srcString, String destString)
            throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                srcString));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(destString));
    
        int by = 0;
        while ((by = bis.read()) != -1) {
            bos.write(by);
        }
    
        bos.close();
        bis.close();
    }
    
    //2.基本字节流一次读取一个字节数组
    private static void method2(String srcString, String destString)
            throws IOException {
        FileInputStream fis = new FileInputStream(srcString);
        FileOutputStream fos = new FileOutputStream(destString);
    
        byte[] bys = new byte[1024];
        int len = 0;
        while ((len = fis.read(bys)) != -1) {
            fos.write(bys, 0, len);
        }
    
        fos.close();
        fis.close();
    }
    
    //1.基本字节流一次读取一个字节
    private static void method1(String srcString, String destString)
            throws IOException {
        FileInputStream fis = new FileInputStream(srcString);
        FileOutputStream fos = new FileOutputStream(destString);
    
        int by = 0;
        while ((by = fis.read()) != -1) {
            fos.write(by);
        }
    
        fos.close();
        fis.close();
    }
    

    实践:

    1.数据复制

    (A.)数据源是文件 目的是文件

    /**
     * 数据源   目的地 
     * 
     * a.txt   b.txt
     */
    public static void main(String[] args) throws IOException {
            
        BufferedReader bufferedReader = new BufferedReader(new FileReader("a.txt"));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("b.txt"));
        String string = null;
        while ((string = bufferedReader.readLine())!=null) {
            bufferedWriter.write(string);
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    (B.)数据源是文件 目的地是控制台

    /**
     * 数据源   目的地 
     * 
     * a.txt   控制台
     */
    public static void main(String[] args) throws IOException {
        
        BufferedReader bufferedReader = new BufferedReader(new FileReader("a.txt"));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
        String string = null;
        while ((string = bufferedReader.readLine())!=null) {
            bufferedWriter.write(string);
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    (C.)数据源是键盘录入 目的地是文本文件

    /**
     * 数据源   目的地 
     * 
     * 键盘录入 a.txt
     */
    public static void main(String[] args) throws IOException {
        
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("a.txt"));
        
        String string = null;
        while ((string = bufferedReader.readLine())!=null) {
            if ("over".equals(string)) {
                break;
            }
            bufferedWriter.write(string);
            bufferedWriter.newLine();
            bufferedWriter.flush();
        }
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    (D.)数据源键盘录入 目的地控制台

    /**
     * 数据源   目的地 
     * 
     * 键盘录入  控制台
     */
    public static void main(String[] args) throws IOException {
        
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
        String string = null;
        while ((string = bufferedReader.readLine())!=null) {
            if ("over".equals(string)) {    
                break;
            }   
            bufferedWriter.write(string);
            bufferedWriter.newLine();
            //bufferedWriter.flush();
        }
        bufferedReader.close();
        bufferedWriter.close();
    }
    

    (E.)键盘录入数据写到文本文件a.txt,然后把a.txt的数据写到b.txt,最后把b.txt的数据显示到控制台(录入---->文件--->文件---->控制台)

    public static void main(String[] args) throws IOException {
        
        //第一步: 键盘录入数据写到文本文件a.txt   数据源--->键盘录入  目的地--->a.txt
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter aBufferedWriter = new BufferedWriter(new FileWriter("a.txt"));
        fileWR(bufferedReader, aBufferedWriter);
        
        //第二步: a.txt的数据写到b.txt   数据源--->a.txt  目的地--->b.txt
        BufferedReader aBufferedReader = new BufferedReader(new FileReader("a.txt"));
        BufferedWriter bBufferedWriter = new BufferedWriter(new FileWriter("b.txt"));
        fileWR(aBufferedReader, bBufferedWriter);
    
        //第三步:b.txt的数据显示到控制台
        BufferedReader bBufferedReader = new BufferedReader(new FileReader("b.txt"));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
        fileWR(bBufferedReader,bufferedWriter);
    }
    public static void fileWR(BufferedReader reader , BufferedWriter writer) throws IOException {
        String string = null;
        while ((string = reader.readLine())!=null) {
            
            if ("over".equals(string)) {    
                break;
            }
            writer.write(string);
            writer.newLine();
            //writer.flush();
        }
        writer.close();
        reader.close();
    }
    

    (F.)数据源 ArrayList 目的地a.txt (集合到文件)

    public static void main(String[] args) throws IOException  {
    
        ArrayList<String> list = new ArrayList<String>();
        list.add("hello");
        list.add("word");
        list.add("java");
    
        BufferedWriter writer = new BufferedWriter(new FileWriter("a.txt"));
    
        for(String string : list){
            writer.write(string);
            writer.newLine();
            writer.flush();         
        }
        writer.close();
    }
    

    (G.)数据源 a.txt 目的地ArrayList (文件到集合)

    public static void main(String[] args) throws IOException  {
    
        BufferedReader reader = new BufferedReader(new FileReader("a.txt"));
        
        ArrayList<String> list = new ArrayList<String>();
        String string = null;
        while ((string = reader.readLine())!=null) {
            list.add(string);
        }
        for(String string2 : list){
            System.out.println(string2);
        }
        reader.close();
    }
    

    (F.)复制单击文件夹

        public static void main(String[] args) throws IOException {
          File srcFile = new File("/Users/xz/Desktop/java_api");
        if (!srcFile.exists()) {
            System.out.println("找不到源文件");
            srcFile.exists();
        }
        File[] fileArr =  srcFile.listFiles();
        
        File desFile = new File("/Users/xz/Desktop/java_api_fuben");
        if (!desFile.exists()) {
            desFile.mkdirs();
        }
        
        for(File fi:fileArr){
        
            System.out.println(fi.getName());
            File dessFile = new File(desFile, fi.getName());
            Copy(fi, dessFile);
        }
    }
    
    public static void Copy(File file , File desFile) throws IOException {
    
        BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
        
        BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(desFile));
        
        byte[] by  = new byte[1024];
        int i = 0;
        while ((i = inputStream.read(by))!= -1) {
    
            outputStream.write(i);
        }
        
        inputStream.close();
        outputStream.close();
        
    }

    相关文章

      网友评论

        本文标题:java 学习笔记之 IO流笔记

        本文链接:https://www.haomeiwen.com/subject/wiwdpttx.html