IO—》字节流&字符流

字节流

一、字节输出流OutputStream

OutputStream此抽象类,是表示输出字节流的所有类的超类。操作的数据都是字节,定义了输出字节流的基本共性功能方法。

FileOutputStream

OutputStream有很多子类,其中子类FileOutputStream可用来写入数据到文件。

FileOutputStream类,即文件输出流,是用于将数据写入 File的输出流。

构造方法:

给文件中续写和换行

构造方法:

实例:

public class OutPutStreamDemo {
    public static void main(String[] args) throws IOException {
        //FileOutputStream的构造方法,如果文件存在,则覆盖
        //如果文件不存在,则给你创建
        FileOutputStream fos=new FileOutputStream("e:\test\bian.text",true);
        /*fos.write(49);
        fos.write(48);
        fos.write(48);*/
        byte[] bytes={-65,-66,-67,-68};//负数汉字,正数阿斯克吗表
        fos.write(bytes,1,2);
        fos.write("bianxiutong
".getBytes());//转成字节数组
        new String(bytes);//再转回字符串
        fos.close();
    }
}

 IO异常的处理

public static void main(String[] args) {
    //释放资源全部放到finally里
    //有异常一般是硬件上出现了问题
    FileOutputStream fos=null;
    try{
        fos=new FileOutputStream("e:\test\demo.text");
        fos.write(100);
    }catch(IOException ex){
        throw new RuntimeException("文件写入失败");
    }finally {
        try {
            if(fos!=null)
            fos.close();
        } catch (IOException ex) {
            throw new RuntimeException("文件关闭失败");
        }
    }
}

二、字节输入流InputStream

如何想把内存中的数据读到内存中,我们通过InputStream可以实现。InputStream此抽象类,是表示字节输入流的所有类的超类。定义了字节输入流的基本共性

功能方法。

int read():读取一个字节并返回,没有字节返回-1.

int read(byte[]): 读取一定量的字节数,并存储到字节数组中,返回读取到的字节数。

FileInputStream

InputStream有很多子类,其中子类FileInputStream可用来读取文件内容。

FileInputStream 从文件系统中的某个文件中获得输入字节。

构造方法:

FileInputStream类读取数据read方法

在读取文件中的数据时,调用read方法,实现从文件中读取数据

public static void main(String[] args) throws IOException{
        FileInputStream fis=new FileInputStream("e:\test\haha.txt");
        int len=0;
        while((len=fis.read())!=-1){
            System.out.print((char)len);//避免指针下移,所以要定义一个成员变量来保留获取到的当前字节
        }
        /*int a=fis.read();
        System.out.println((char)a);//将字节转换成字符
        int b=fis.read();
        System.out.println((char)b);
        int c=fis.read();
        System.out.println((char)c);
        int d=fis.read();
        System.out.println((char)d);
        int e=fis.read();
        System.out.println((char)e);*/
        fis.close();
    }

字符流

明确什么时候用字符流:当处理打开后的txt能看懂的文本文档时才用字符流

实例:

public class CharStreamDemo {
    public static void main(String[] args) throws IOException {
        //给文件中写中文
        writeCNText();
        //读取文件中的中文
        readCNText();
    }    
    //读取中文
    public static void readCNText() throws IOException {
        FileInputStream fis = new FileInputStream("c:\cn.txt");
        int ch = 0;
        while((ch = fis.read())!=-1){
            System.out.println(ch);
        }
    }
    //写中文
    public static void writeCNText() throws IOException {
        FileOutputStream fos = new FileOutputStream("c:\cn.txt");
        fos.write("欢迎你".getBytes());
        fos.close();
    }
}

字符输入流reader

read():读取单个字符并返回

read(char[]):将数据读取到数组中,并返回读取的个数。

FileReader

构造方法:

public static void method1() throws IOException{
    FileReader fr=new FileReader("e:\test\haha.txt");
    int len=0;
    while((len=fr.read())!=-1){
        System.out.print((char)len);
    }
    fr.close();
}
public static void method2() throws IOException{
    FileReader fr=new FileReader("e:\test\haha.txt");
    int len=0;
    char[] ch=new char[1024];
    while((len=fr.read(ch))!=-1){
        System.out.println(new String(ch,0,len));
    }
    fr.close();
}

字符输入流writer

FileWriter

构造方法:

FileWriter写入中文到文件中

public static void main(String[] args) throws IOException {
    //写入中文字符
    FileWriter fw=new FileWriter("e:\test\haha.txt");
    fw.write("萨曼莎");
    //fw.close();
    fw.flush();
    //flush():将流中的缓冲区缓冲的数据刷新到目的地中,刷新后,流还可以继续使用。
    //close():关闭资源,但在关闭前会将缓冲区中的数据先刷新到目的地,否则丢失数据,然后在关闭流。流不可以使用。
    //如果写入数据多,一定要一边写一边刷新,最后一次可以不刷新,由close完成刷新并关闭。
}

练习:

分别利用字节流和字符流复制同一张图片

public static void main(String[] args) throws IOException {
    //method1();
    method2();
}
//字节
public static void method1() throws IOException{
    FileInputStream fis=new FileInputStream("e:\test\WallpaperStudio10-43897.jpg");
    FileOutputStream fos=new FileOutputStream("e:\test\text\haha.jpg");
    int len=0;
    byte[] bytes=new byte[1024];
    while((len=fis.read(bytes))!=-1){
        fos.write(bytes,0,len);
    }
    fis.close();
    fos.close();
}
    //字符
public static void method2() throws IOException{
    FileReader fr=new FileReader("e:\test\WallpaperStudio10-43897.jpg");
    FileWriter fw=new FileWriter("e:\test\text\hahaha.jpg");
    int len=0;
    char[] ch=new char[1024];
    while((len=fr.read(ch))!=-1){
        fw.write(ch,0,len);
    }
    fr.close();
    fw.close();
}
//结论:字节可以打开,字符打不开

要注意:

只有txt文档才需要用字符流来进行读写操作,其他都用字节流

原文地址:https://www.cnblogs.com/Ace-suiyuan008/p/9510366.html