字节流与字符流——Java

Java.io包流分为两类:输入流与输出流

字节流(单位:byte):原生操作,无需转化,可以处理文本文件、图像、音乐、视频等资源

              InputStream:字节输入流

             OutputStream:字节输出流

字符流(单位:char):经过处理后的操作,只用于处理中文文本

          Reader:字符输入流

          Writer:字符输出流

    以终端(所在的程序)为参考系的输入/出

 

流(字节流)媒体文件:视频和音频(由一个个的字节组成的)

流模型的操作流程:

            1、取得终端对象

            2、根据终端对象取得输入输出流

            3、根据输入输出流进行数据的读取与写入

           4、关闭流

I/O操作属于资源处理:在使用后一定要关闭,

资源处理:I/O操作,数据库操作,网络处理

 

1、字节流:

1.1、OutputStream:字节输出流

public abstract class OutputStream implements Closeable,Flushable    @since  JDK1.0

Closeable: public void close() throws IOException; 关闭流方法

Flushable:public void flflush() throws IOException; 强制刷新缓冲区

核心方法:

public void write(byteb[]) throws IOException{  //将给定的字节数组全部输出

    write(b,0,b.length);

}

 

public abstract void write(int b)throws IOException;  //输出单个字节
public void write(byte b[],int off,int len) throws IOException{ 
    //将给定的字节数组以off位置开始输出len长度后停止输出
    if(b==null){
        Throw new NullPointerException();
    }else if((off<0)||(off>b.length)||(len<0)||((off+len)>b.length)||((off+len)<0)){
        Throw new IndexOutOfBoundsException();
    }else if(len==0){
        return;
    }
    for(int i=0;i<len;i++){
        write(b[off+i]);
    }
}

 

使用OutputStream输出数据时,若指定的文件不存在,FileOutputStream会自动创建文件(不包含创建目录)

public static void main(String[]args) throws IOException{
    //1、取得终端对象
    File file = new File("C:\\Users\\78733\\Desktop\\text.txt");
    //2、根据终端对象取得输入输出流
    OutputStream outputStream = new FileOutputStream(file);
    //3、根据输入输出流进行数据的读取与写入
    String str = "hello";    //String中一个字母占一个字节
    outputStream.write(str.getBytes());
    //4、关闭流
    outputStream.close();
}

使用FileOutputStream输出内容时,默认时文件内容的覆盖操作

如果进行文件内容的追加,使用如下构造方法

public FileOutputStream(File file,boolean append)

 

Jdk1.7追加了AutoCloseable自动关闭接口,要使用此接口,必须使用try-catch代码块,推荐显示关闭

public interface AutoCloseable

public interface Closeable extends AutoCloseabl

 

1.2、字节输入流:InputStream

public abstract class InputStream implements Closeable

public abstract int read() throws IOException;

public int read(byteb[])throws IOException{  //将读取的内容放在字节数组中,返回值有如下3种情况
    return read(b,0,b.length);                          
}

 返回值含义                     

1、返回b.length

2、返回大于0的整数,此整数小于b.length

3、返回-1  终止标记

1、总未读的数据大小比缓冲区要大,所以刚开始读取的数据为缓冲区的大小,为b.length,此时返回b.length

2、此时未读取的数据小于存放的缓冲区大小,返回剩余数据大小,此时返回大于0的整数,此整数小于b.length

3、此时数据已经读取完毕,返回-1

总的来说就是每次读到的数据大小,没读到时返回-1

就像这个碗一样,碗的大小就是数据的大小,勺子就是缓冲区的大小,每次读取数据时只能从缓冲区读,就像每次吃到嘴里的饭最多只有勺子这么大,最后一勺子的饭最大为勺子的大小。饭吃完了,返回-1.

 

public int read(byte b[],int off,int len)throws IOException{
    if(b==null){
        Throw new NullPointerException();
    }else if(off<0||len<0||len>b.length-off){
        Throw new IndexOutOfBoundsException();
    }else if(len==0){
        return 0;
    }
    int c=read();
    if(c==-1){
        return-1;
    }
    b[off]=(byte)c;
    int i=1;
    try{
        for(;i<len;i++){
            c=read();
            if(c==-1){
                break;
            }
            b[off+i]=(byte)c;
        }
    }catch(IOExceptione e){}
    return i;
}

2、字符流:关于中文文本处理比较好

2.1、字符输出流:Writer:适用于处理中文文本,覆盖输入(之前的内容清空)

public abstract class Writer implements Appendable,Closeable,Flushable     @since  JDK1.1

public void write(String str) throws IOException{   //可以直接支持字符串输出
    write(str,0,str.length());
}

字符流若未关闭,数据在缓冲区存放,不会输出到目标终端。要想将数据输出,要么将输出流关闭(自动到达终端),要么使用flush()强制刷新缓冲区。

 

2.2、字符输入流:Reader

public abstract class Reader implements Readable,Closeable   @sinceJDK1.1

public int read(char cbuf[]) throws IOException{
    return read(cbuf,0,cbuf.length);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值