IO处理流

package _03;

import java.io.*;

public class _3_23BufferInputStream {
    public static void main(String[] args) {
//        1.性能
//        2.底层还是节点流
//        3.直接关闭处理流,内部自动关闭内部流
//        原理:提供默认的缓存,默认缓存内存为8k,也可以自己设定,当缓存满了则再进行传输

        File file = new File("D:\\life\\demo.txt");
        InputStream is = null;
        try {
            is = new BufferedInputStream(new FileInputStream(file));
            byte[] flush = new byte[1024];  //容器大小
            int length = -1;//接收长度
            while((length=ism.read(flush))!=-1){
                //将字节数组转换成字符
                String temp = new String(flush,0,length,"utf-8");
                System.out.print(temp);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(is!=null){
                    is.close();
                }

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

    }

}

package _03;

import java.io.*;
import java.net.URL;

public class _3_23InputStreamReader {
    public static void main(String[] args) {
//        InputStreamReader 字节流和字符流之间的桥梁,将字节流转成字符流
//        为字节流指定字符集

//        1.操作System.in and System.out
//        InputStreamReader isr = new InputStreamReader(System.in);
//        try(BufferedReader bis = new BufferedReader( new InputStreamReader(System.in));
//        BufferedWriter osw = new BufferedWriter(new OutputStreamWriter(System.out));){  //使用try...with..resouce 不需要释放资源
//            循环获取键盘输入,输出此内容
//            String msg = "";
//            while(!msg.equals("exit")){
//                msg = bis.readLine();
//                osw.write(msg);
//                osw.newLine();
//                osw.flush(); // osw有缓存空间,没有达到缓存空间的容量 它不会输出
//            }
//        }catch(IOException e){
//            e.printStackTrace();
//        }

//        2.操作网络流
        try(BufferedReader is = new BufferedReader(new InputStreamReader(new URL("http://www.baidu.com").openStream(),"utf-8"));
            BufferedWriter ow = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("d:\\baidu.txt"),"utf-8"));){   //网络节点 字节流
//            int temp;
//            while((temp=is.read())!=-1){
//                System.out.print((char)temp);
//            }
           String msg = "";
           while((msg=is.readLine())!=null){
               ow.write(msg);
               ow.newLine();
           }
           ow.flush();

        }catch(IOException e){

        }

    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值