IO详解 常用io类

InputStream与Reader

       前者是字节输入流,读取文件内容,文件需要先转换成byte,才能读取得到,如果是中文,可能会出现乱码;后者是字符输入流,可以读取字符串,特别是有中文的时候,可以使用此类,不会出现乱码。

OutputStream 与Writer

      前者是字节输出流,数据保存到文本中,需要先转换成byte,有中文会出现乱码;后者是字符输出流,字符串可以直接写入到文本中,不需要转换成byte,中文不会出现乱码

基本关系图



1:InputStream与FileInputStream基本引用

public static void main(String[] args) throws IOException {
    String str=new String();
    InputStream in=new FileInputStream(new File("F:\\t.txt"));
    byte[] bytes=new byte[1024];
    int i=0;
    while ((i=in.read(bytes))!=-1){
        str+=new String(bytes,0,i);
    }
    in.close();
}

2:BufferInputStream

BufferInputStream是一个字节缓冲流,读取的速率能远远大于FileInputStream,对应大文件会有很明显的效果

        
public static void main(String[] args) throws IOException, ParseException {
    String str=new String();
    InputStream in=new FileInputStream(new File("F:\\t.txt"));
    BufferedInputStream buffered=new BufferedInputStream(in);
    byte[] bytes=new byte[1024];
    int i=0;
    while ((i=buffered.read(bytes))!=-1){
        str+=new String(bytes,0<
  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值