java基础io流

     IO流概述

      可以将这种数据传输操作,看做一种数据的流动 , 按照流动的方向分为输入Input和输出Output
     Java中的IO操作主要指的是 java.io包下的一些常用类的使用. 通过这些常用类对数据进行读取(输入Input) 和 写出(输出Output)
    IO流的分类:
     按照流的方向来分,可以分为:输入流和输出流.
     按照流动的数据类型来分,可以分为:字节流和字符流
      字节流:
             -   输入流 :   InputStream
             -   输出流 :   OutputStream
         字符流:
              -   输入流 :   Reader
              -   输出流 :   Writer
     一切皆字节:
           计算机中的任何数据(文本,图片,视频,音乐等等)都是以二进制的形式存储的.
          在数据传输时 也都是以二进制的形式存储的.
           后续学习的任何流 , 在传输时底层都是二进制.

java输入/输出流体系中常用的流

表中粗体字所标出的类代表节点流,必须直接与指定的物理节点关联:斜体字标出的类代表抽象基类,无法直接创建实例。

InputStream和Reader方法

int read(); 从输入流中读取单个字节,返回所读取的字节数据(字节数据可直接转换为int类型)。
int read(byte[] b)从输入流中最多读取b.length个字节的数据,并将其存储在字节数组b中,返回实际读取的字节数。
int read(byte[] b,int off,int len); 从输入流中最多读取len个字节的数据,并将其存储在数组b中,放入数组b中时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字节数。
在Reader中包含如下3个方法。
int read(); 从输入流中读取单个字符,返回所读取的字符数据(字节数据可直接转换为int类型)。
int read(char[] b)从输入流中最多读取b.length个字符的数据,并将其存储在字节数组b中,返回实际读取的字符数。
int read(char[] b,int off,int len); 从输入流中最多读取len个字符的数据,并将其存储在数组b中,放入数组b中时,并不是从数组起点开始,而是从off位置开始,返回实际读取的字符数。

OutputStream和Writer方法

void write(int c); 将指定的字节/字符输出到输出流中,其中c即可以代表字节,也可以代表字符。
void write(byte[]/char[] buf); 将字节数组/字符数组中的数据输出到指定输出流中。
void write(byte[]/char[] buf, int off,int len ); 将字节数组/字符数组中从off位置开始,长度为len的字节/字符输出到输出流中。

文件输入流FileInputStream/FileReader

使用FileInputStream读取文件:

 FileInputStream fis=null;
      try {
         
          fis=new FileInputStream("C:Test.txt");
        
          byte[] b=new byte[1024];
       
          int hasRead=0;
         
          while((hasRead=fis.read(b))>0){
             
            System.out.print(new String(b,0,hasRead));
          }
      }catch (IOException e){
        e.printStackTrace();
      }finally {
          fis.close();
      }

使用FileReader读取文件

public  static void main(String[] args)throws IOException{
        FileReader fis=null;
        try {
            fis=new FileReader("C:Test.txt");
            char[] b=new char[1024];
            //用于保存的实际字节数
            int hasRead=0;  
            while((hasRead=fis.read(b))>0){   
                System.out.print(new String(b,0,hasRead));
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            fis.close();
        }
    }
}

FileOutputStream的用法:

 FileInputStream fis=null;
 FileOutputStream fos=null;
        try {
            //创建字节输入流
            fis=new FileInputStream("C:\\Test.txt");
            //创建字节输出流
            fos=new FileOutputStream("C:\\newTest.txt");
            byte[] b=new byte[1024];
            int hasRead=0;
            //循环从输入流中取出数据
            while((hasRead=fis.read(b))>0){
                //每读取一次,即写入文件输入流,读了多少,就写多少。
                fos.write(b,0,hasRead);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            fis.close();
            fos.close();
        }

缓冲流(BufferedInputStream/BufferedReader,BufferedOutputStream/BufferedWriter):

FileInputStream fis=null;
        FileOutputStream fos=null;
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;
        try {
            //创建字节输入流
            fis=new FileInputStream("C:\\Test.txt");
            //创建字节输出流
            fos=new FileOutputStream("C:\\newTest.txt");
            //创建字节缓存输入流
            bis=new BufferedInputStream(fis);
            //创建字节缓存输出流
            bos=new BufferedOutputStream(fos);
            byte[] b=new byte[1024];
            int hasRead=0;
            //循环从缓存流中读取数据
            while((hasRead=bis.read(b))>0){
                //向缓存流中写入数据,读取多少写入多少
                bos.write(b,0,hasRead);
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            bis.close();
            bos.close();
        }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值