IO流中字符流和功能流

  • 字符流 :只能读写纯文本内容 功能分:节点流

  • Reader 字符输入流 FileReader 文件字符输入流

  • Writer 字符输出流 FileWriter 文件字符输出流

  • 图片的拷贝:
    */
    public class CharDemo01 {
    public static void main(String[] args) throws IOException {
    //1.选择流
    Reader read=new FileReader(“D:/test.txt”);
    Writer write=new FileWriter(“E:/hehe.txt”);
    //2.读取
    char[] car=new char[1024];
    int len;
    /int num=read.read();
    System.out.println((char)num);
    System.out.println((char)read.read());
    /
    while((len=read.read(car))!=-1){
    write.write(car, 0, len);
    }
    //3.刷出
    write.flush();

    //3.关闭
    write.close();
    read.close();
    

    }
    }

  • 字符流 字节流 -->节点流

  • 包裹节点流,在节点流之上–>功能流|处理流

  • 处理流:增强功能,提高性能的作用,在节点流之外才能使用,不能代替节点流

  • 缓冲流:提高性能

  • 字节缓冲流 BufferedInputStream BufferedOutputStream 无新增方法
    */
    public class BufferedDemo01 {
    public static void main(String[] args) {
    //1.建立联系
    File src=new File(“D:/test.txt”);
    File dest=new File(“E:/test2.txt”);
    //2.选择流
    InputStream is=null;
    OutputStream out=null;
    try {
    is=new BufferedInputStream(new FileInputStream(src));
    out=new BufferedOutputStream(new FileOutputStream(dest));
    //3.操作
    byte[] car=new byte[1024];
    int len=-1;
    while((len=is.read(car))!=-1){
    out.write(car, 0, len);
    }
    //4.刷出
    out.flush();

    } catch (FileNotFoundException e) {
    	e.printStackTrace();
    } catch (IOException e) {
    	// TODO Auto-generated catch block
    	e.printStackTrace();
    } finally{
    	//5.关闭
    	if(out!=null){
    		try {
    			out.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    	if(is!=null){
    		try {
    			is.close();
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    	}
    }
    

    }
    }

  • 字符缓冲流 不能发生多态,因为存在新增方法

  • BufferedReader 字符输入缓冲流 新增方法 readLine() 读取一行

  • BufferedWriter 字符输出缓冲流 新增方法 newLine() 写出换行符
    */
    public class BufferedDemo02 {
    public static void main(String[] args) throws IOException {
    //1.选择流
    BufferedReader read=new BufferedReader(new FileReader(“D:/test.txt”));
    BufferedWriter write=new BufferedWriter(new FileWriter(“E:/hehe.txt”));
    //2.读取
    // String str=read.readLine();
    String s=null;
    while((s=read.readLine())!=null){
    write.write(s);
    write.newLine();
    }
    //3.刷出
    write.flush();

    //3.关闭
    write.close();
    read.close();
    

    }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值