Java学习之过滤流的作用

Java学习之过滤流的作用

一、定义

过滤流:BufferedInputStreamBufferedOutputStream, 缓存作用,用于装配文件磁盘、网络设备、终端等读写开销大的节点流,提高读写性能

二、使用

1. 过滤流BufferedReader的使用:用于缓存字符流,可以一行一行的读。

举例:

import java.io.*;
public class inDataSortMaxMinIn { 
    public static void main(String args[]) {
     try{
        BufferedReader keyin = new BufferedReader(new 
                                    InputStreamReader(System.in)); 
        String c1;
        int i=0;
        int[] e = new int[10];   
        while(i<10){
           try{
               c1 = keyin.readLine();
               e[i] = Integer.parseInt(c1);
               i++;
            }  
            catch(NumberFormatException ee){
                   System.out.println("请输入正确的数字!");
            }
       }
    }
    catch(Exception e){
        System.out.println("系统有错误");
 }
    }
}

2. 过滤流:DataInputStreamDataOutputStream, 可从字节流中写入、读取Java基本数据类型,不依赖于机器的具体数据类型,方便存储和恢复数据

举例:

import java.io.*;
public class DataStream {
  public static void main(String[] args)throws Exception{
   try {
    DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("test.txt")));
    dos.writeInt(3);//写入整型
    dos.writeDouble(3.14);//写入浮点型
    dos.writeUTF(“hello”);//写入字符串
    dos.close();
     
    DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("test.txt")));
    System.out.println(dis.readInt()); //读取整型,输出3
    System.out.println(dis.readDouble()); //读取浮点型,输出3.14
    System.out.println(dis.readUTF()); //读取字符串,输出hello
    dis.close();
   } catch (FileNotFoundException e) {
         e.printStackTrace();
    }
  }
}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值