zoukankan      html  css  js  c++  java
  • IO流

    IO流:

      定义:

        IO流意思是输入输出流, 用来实现将文件中的数据写入内存, 或者将内存中的数据读取到文件当中

        如果数据输入到内存中称为输入流

        如果从内存中输出数据称为输出流

      分为字节流和字符流

      字节流即以单个字节byte(8bit)的形式进行传输, 通常带有Stream的都是字节流

      字符流即以字符的形式进行传输,通常带有writer,reader的都是字符流

    分类:

      按流向区分:

        输入流: 

          inputStream

          Reader -- 我的理解是内存从磁盘中读取数据,所以是Reader,即输入流

        输出流: 

          outputStream

          Writer -- 我的理解是将内存中的数据写入磁盘中,所以是Writer,即输出流

       按读写类型区分

        字节流

          inputStream   字节输入流

          outputStream   字节输出流

        字符流

          Reader    字符输入流

          Writer      字符输出流

        缓冲流 --- (效率和利用率高)

          BufferedInputStream  缓冲字节输入流

          BufferedOutputStream  缓冲字节输出流

          BufferedReader  缓冲字符输入流

          BufferedWriter  缓冲字符输出流

        

    案例一 : 使用字节流将数据写入文档

      

    /**
    * 使用字节流,
    * 将HelloWorld写入D:UsersAdministratorIdeaProjectsaaa新建文本文档.txt中
    */
    private static void method1() {
    try {
    FileOutputStream fos = new FileOutputStream("D:\Users\Administrator\IdeaProjects\aaa\新建文本文档.txt",true);
    String str = "Hello World";
    byte[] bytes = str.getBytes();
    fos.write(bytes,0,bytes.length);
    fos.close();  //IO流在使用完成后记得释放资源close(); 否则会一直消耗系统资源.
    }catch(Exception e){
    e.printStackTrace();
    }
    }

    效果:

       

     案例二 : 使用字节流读取文档数据

      /**
         * 使用字节流
         *      读取文件内容 -- outputStream
         */
        private static void method2() {
            try {
                FileInputStream fis = new FileInputStream("D:\Users\Administrator\IdeaProjects\aaa\新建文本文档.txt");
                byte[] bytes = new byte[1024];    //一次读取1024个字节
                fis.read(bytes);
                System.out.println(new String(bytes));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

     效果:

      

    案例三 : 使用字符流读取文档数据

        /**
         * 使用字符流读取文档数据
         */
        private static void method3(){
            try {
                FileReader fr = new FileReader("D:\Users\Administrator\IdeaProjects\aaa\新建文本文档.txt");
                BufferedReader br = new BufferedReader(fr);
                String line = br.readLine();
                for (int i = 2;i>1;i++) {
                    if (line != null) {
                        System.out.println(line);
                        line = br.readLine();
                    }
                }
                fr.close();
                br.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    案例四 : 使用字符流写入数据

         /**
         * 使用字符流写入数据
         */
        private static void method4() {
            try {
                FileWriter fw = new FileWriter("D:\Users\Administrator\IdeaProjects\aaa\新建文本文档.txt",true);
                BufferedWriter bw = new BufferedWriter(fw);
    //            System.out.println("请输入文字: ");
                while (true) {
                    String line = sc.nextLine();
                    if (!(line.equals("over"))) {
                        bw.write(line + "
    ");
                        bw.flush();
                    } else {
                        break;
                    }
                }
                fw.close();
                bw.close();
    
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
  • 相关阅读:
    python笔试题(3)
    跨域资源共享 CORS 详解
    RequestDispatcher接口include方法
    清除浮动的四种方式及其原理理解
    css---vw布局来适应移动端【解决rem的bug】
    golang 实现简单的rsa加密算法
    golang 查找素数
    JAVA大文件上传支持分片上传
    JSP大文件上传支持切割上传
    JSP大文件上传支持分场上传
  • 原文地址:https://www.cnblogs.com/myBlog-ahao/p/10923548.html
Copyright © 2011-2022 走看看