• 梳理15--流


    详细 百度和文档

    https://www.cnblogs.com/shitouer/archive/2012/12/19/2823641.html

    1. 

    2.

    3.

    is = new FileInputStream("D://a/a/a.txt");
                os = new FileOutputStream("D://a/b/a.txt");
    
                //装东西的小桶
                byte[] buffer = new byte[1024];
    //
    byte[] buffer = new byte[1024*20];
    int len; while((len = is.read(buffer)) != -1){ //从buffer的0开始,到len结束 os.write(buffer,0,len); }
       /**
         * 拷贝文件
         */
        @Test
        public void copyFile() throws IOException {
            InputStream is = null;
            OutputStream os = null;
            try {
    // 1
                is = new FileInputStream("D://a/a/a.txt");
                os = new FileOutputStream("D://a/b/a.txt");
    
                //装东西的小桶
                byte[] buffer = new byte[1024];
                int len;
                while((len = is.read(buffer)) != -1){
                    //从buffer的0开始,到len结束
                    os.write(buffer,0,len);
                }
    
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
    // 2
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
    //3
                if (os != null) {
                    try {
                        os.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    拷贝文件
     File file = new File("D://a/a/a.txt");
                long length = file.length();
                //一次20k
                int per = 1024*20;
                //已经拷贝完成的大小
                long completed = 0L;
                long currentProgress = 0L;
    
                is = new FileInputStream("D://a/a/a.txt");
                os = new FileOutputStream("D://a/b/a.txt");
    
                //装东西的小桶
                byte[] buffer = new byte[per];
                int len;
                while((len = is.read(buffer)) != -1){
                    //从buffer的0开始,到len结束
                    os.write(buffer,0,len);
                    completed += per;
    
                    //显示进度
                    double percent = (((double)completed/(double) length)*100);
                    long progress = Math.round(percent);
                    if (progress != currentProgress){
                        System.out.println("已经拷贝了:"+ progress + "%");
                    }
                    currentProgress = progress;
                }
    打印进度

    4. 读文件

    //放一个管道到文件上

    //整一个缓冲

    //写出去
     /**
         * 读文件
         * @throws Exception
         */
        @Test
        public void readFile() throws Exception {
            InputStream is = new FileInputStream("D://a.txt");
            byte[] buffer = new byte[1024];
            int len;
            while((len = is.read(buffer)) != -1){
                String s = new String(buffer, 0,len);
                System.out.println(s);
            }
    
            is.close();
        }
    字节流
        /**
         * 读文件
         * @throws Exception
         */
        @Test
        public void readFile() throws Exception {
            Reader reader = new FileReader("D://a.txt");
            char[] buffer = new char[1024];
            int len;
            while((len = reader.read(buffer)) != -1){
                String s = new String(buffer, 0,len);
                System.out.println(s);
            }
    
            reader.close();
        }
    字符流

    字节流可以处理任何字符流的东西。

        /**
         * 读文件
         * @throws Exception
         */
        @Test
        public void readFile() throws Exception {
            Reader reader = new FileReader("D://a.txt");
            BufferedReader bf = new BufferedReader(reader);
            String str;
            while ((str = bf.readLine()) != null){
                System.out.println(str);
            }
            reader.close();
        }
    处理流

    处理流 一行一行的读

    5. 写文件

     @Test
        public void writeFile() throws Exception{
            //放一个管道到文件上
            FileOutputStream fo = new FileOutputStream("D://a.txt");
            String str = "hello World";
    
            //写出去
            fo.write(str.getBytes(StandardCharsets.UTF_8));
    
            fo.flush();
            fo.close();
        }
    字节流
    @Test
        public void writeFile() throws Exception{
            //放一个管道到文件上
            FileWriter fw = new FileWriter("D://a.txt");
            String str = "hello World";
    
            //写出去
            fw.write(str);
    
            fw.flush();
            fw.close();
        }
    字符流
    fw.append("aa").append("bb");
  • 相关阅读:
    为 ADO 程序员设计的 ADO.NET (转)
    MSN 历史纪录分页显示(XML + XSL + Javascript)
    python连接postgresql数据库
    centOS安装scikitlearn
    14. 页高速缓存 20100228 21:10 322人阅读 评论(0) 收藏
    18. 程序的执行 20100302 12:56 131人阅读 评论(0) 收藏
    ttt 20110118 11:49 71人阅读 评论(0) 收藏
    15. 访问文件 20100228 22:55 129人阅读 评论(0) 收藏
    17. 进程间通信 20100302 12:49 191人阅读 评论(0) 收藏
    19(终). 系统启动 20100302 13:00 191人阅读 评论(0) 收藏
  • 原文地址:https://www.cnblogs.com/Master-Sun/p/14295409.html
Copyright © 2020-2023  润新知