码迷,mamicode.com
首页 > 其他好文 > 详细

IO流

时间:2019-05-25 20:18:22      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:input   img   print   rac   available   eth   内存   div   lin   

IO流:

  定义:

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

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

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

  分为字节流和字符流

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

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

分类:

  按流向区分:

    输入流: 

      inputStream

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

    输出流: 

      outputStream

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

 

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

  

/**
* 使用字节流,
* 将HelloWorld写入D:\Users\Administrator\IdeaProjects\aaa\新建文本文档.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[fis.available()];    //avaliable返回的是文件大小,即数组的长度
            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 + "\r\n");
                    bw.flush();
                } else {
                    break;
                }
            }
            fw.close();
            bw.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

IO流

标签:input   img   print   rac   available   eth   内存   div   lin   

原文地址:https://www.cnblogs.com/myBlog-ahao/p/10923548.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!