zoukankan      html  css  js  c++  java
  • 021.9 IO流 流总结

    ###################################################################################
    IO流的规律总结:解决的问题,开发中具体使用哪个流对象的问题
    1,明确数据源,数据目的
        就是在明确要使用的IO体系。InputStream   OutputStream     Reader       Writer
        需求中做为源:意味着是读
        使用:InputStream    Reader
        
        需求中做为目的:意味着是写
        使用:OutputStream    Writer
        
    2,操作的数据是否是纯文本数据
        是,字符流
        否,字节流
        是并且是源,使用Reader
        是并且是目的,Writer
        
    3,明确要操作的具体设备。每个设备都有对应的流对象
        源设备:
            硬盘:能操作File的流对象都是。File开头
            键盘:System.in
            内存:数组
            网络:socket流
        目的设备:
            硬盘:能操作File的流对象都是。File开头
            键盘:System.out
            内存:数组
            网络:socket流
            
    4,是否需要额外的功能
        是否需要高效:缓冲区,Buffered开头
        是否需要编码转换:转换流

    ######################################################################################

    需求1、通过键盘录入数据,保存到一个文件中
        思路:1)明确源和目的,都有,源:IntputStream  Reader              目的:OutputStream     Writer
            2)明确是否是文本,是文本,源:Reader   目的:Writer
            3)具体设备:源:System.in          目的:硬盘

    public static void main(String[] args) throws IOException
    {
        InputStream is = System.in;
        FileWriter fw = new FileWriter("myfile\a.txt");
        byte[] by = new byte[1024];
        int count = 0;
        while((count = is.read(by))!=-1){
            System.out.println(new String(by,0,count));
            fw.write(new String(by), 0, count);
        }
        fw.close();
    }
    1.1

    //但是麻烦,因为明确源是Reader,需要将字节流转成字符流,所以用InputStreamReader

    public static void main(String[] args) throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        FileWriter fw = new FileWriter("myfile\a.txt",true);
        char[] chr = new char[1024];
        int count = 0;
        while((count = isr.read(chr)) != -1){   //因为是键盘输入不会有末端。
            String s = new String(chr,0,count);
            System.out.println(s);
            fw.write(s);
            fw.flush();
        }
        fw.close();
    }
    1.2

    //需要高效,加入缓冲区

    public static void main(String[] args) throws IOException
    {
        InputStreamReader isr = new InputStreamReader(System.in);
        BufferedReader br = new BufferedReader(isr);
        FileWriter fw = new FileWriter("myfile\a.txt",true);
        BufferedWriter bw = new BufferedWriter(fw);
        String line = "";
        while((line = br.readLine()) != null){   //因为是键盘输入不会有末端。
            bw.write(line);
            bw.newLine();
            System.out.println(line);
            bw.flush();
        }
        br.close();
        bw.close();
    }
    1.3

    ###############################################################################################
    需求2:读取文本文件,显示在控制台上
        思路:1)明确源和目的,都有,源:IntputStream  Reader              目的:OutputStream     Writer
            2)明确是否是文本,是文本,源:Reader   目的:Writer
    3)具体设备:源:硬盘          目的:System.out

    public static void main(String[] args) throws IOException
    {
        FileReader fr = new FileReader("myfile\a.txt");
        OutputStream os = System.out;
        char[] chr = new char[1024];
        int count = 0;
        while((count = fr.read(chr)) != -1){
            String s = new String(chr,0,count);
            byte[] by = s.getBytes();                       //因为OutputStream用的是byte[],所以我转成byte类型
            os.write(by);
        }
        System.out.println("//read over!");
    }
    2.1
    public static void main(String[] args) throws IOException
    {
        FileReader fr = new FileReader("myfile\a.txt");
        OutputStreamWriter osw = new OutputStreamWriter(System.out);
        char[] chr = new char[1024];
        int count = 0;
        while((count = fr.read(chr)) != -1){
            osw.write(chr,0,count);         //没有显示在控制台上,需要flush一下
            osw.flush();
        }
    
    }
    2.2
    public static void main(String[] args) throws IOException
    {
        FileReader fr = new FileReader("myfile\a.txt");
        BufferedReader br = new BufferedReader(fr);
        OutputStreamWriter osw = new OutputStreamWriter(System.out);
        BufferedWriter bw = new BufferedWriter(osw);
        String line = "";
        while((line = br.readLine()) != null){
            bw.write(line);
    //        System.out.println(line);
        }
    }
    2.3加入缓冲区
  • 相关阅读:
    短视频直播源码开发,防抖和节流的区别和实用场景
    游戏陪玩平台源码开发,语音通话中的噪音消除处理
    语音聊天室源码开发,如何实现回音消除功能?
    【代码解析】双向链表实现贪吃蛇游戏!简单易学,开发自己第一个游戏!
    程序员偷偷去面试,上班时却没发现身上还有其他公司的访客贴!
    编程语言年度观赏大戏,来看看内部撕X,你站谁?
    数组倒序排列,数组倒置,C语言数组倒序算法详解!
    编程领域这些禁术相当精彩,掌握其一,方可修炼编程大法!
    无处不在的网络编程,到底是如何工作的?今天我们一探究竟!
    【编程黑科技】gethostbyname()函数:通过域名获取IP地址!
  • 原文地址:https://www.cnblogs.com/-nbloser/p/9063602.html
Copyright © 2011-2022 走看看