java学习笔记之IO编程—字节流和字符流

1. 流的基本概念

在java.io包里面File类是唯一一个与文件本身有关的程序处理类,但是File只能够操作文件本身而不能操作文件的内容,或者说在实际的开发之中IO操作的核心意义在于:输入与输出操作。输入和输出实质上传递的就是一种数据流的处理形式,数据流指的是字节数据 。而对于这种流的处理形式在java.io包里面提供有两类支持。

  • 字节处理流:OutputStream(输出字节流)InputStream(输入字节流)
  • 字符处理流:Writer(输出字符流)Reader(输入字符流) 

流操作的统一处理步骤(以文件处理流程为例):

  • 果现在要进行的是文件的读写操作,则一定要通过File类找到一个文件路径
  • 通过字节流或字符流中的方法来实现数据的输入与输出操作
  • 流的操作属于资源操作,资源操作必须进行关闭处理

2.字节输出流:OutputStream类

字节的数据是以byte类型为主实现的操作,在进行字节内容的输出的时候可以使用OutputStream类完成

类的基本定义:

  • public abstract class OutputStream extends Object implements Closeable, Flushable

三个内容输出的方法:

  • 输出一组字节数据:public void write​(byte[] b) throws IOException
  • 输出部分字节数据:public void write​(byte[] b,int off,int len)throws IOException
  • 输出单个字节数据:public abstract void write​(int b)throws IOException

注意:

OutputSream是一个抽象类,如果想要获得实例化对象应该通过对子类实例的向上转型完成,如果说现在要进行的是文件处理操作,则可以使用FileOutputStream子类

FileOutputStream构造方法:

  • public FileOutputStream​(File file)throws FileNotFoundException
  • public FileOutputStream​(File file,boolean append)throws FileNotFoundException

 

范例:使用OutputStream类实现内容输出(符合上述的流操作的统一处理步骤)

import java.io.*;

public class OutputStreamDemo {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("D:"+File.separator+"hello"
        +File.separator+"demo.txt");         //1.指定要操作的文件路径
        if(!file.getParentFile().exists()) { //文件不存在
            file.getParentFile().mkdirs();   //创建父目录
        }
        try (OutputStream output = new FileOutputStream(file,true)) {//2.通过子类实例化
        String str = "hello world\r\n"; //要输出的文件内容
        output.write(str.getBytes());//3.将字符串变为字节数组并输出
        } catch (IOException e) {
            e.printStackTrace();
        }//4.AutoCloseable 自动关闭
        //执行后会自动帮助用户创建文件
    }

}

 

3.字节输入流:InputStream类

类的基本定义:

  • public abstract class InputStream extends Object implements Closeable

核心方法:

  • 读取单个字节数据,如果以及读取到底,返回-1:public abstract int read() throws IOException
  • 读取一组字节数据,返回的是读取的个数,如果没有则返回-1:public int read​(byte[] b) throws IOException
  • 读取一组字节数据的部分内容(只占数组的部分):public int read​(byte[] b,int off,int len) throws IOException

InputoutStream和OutputStream一样都属于抽象类,要依靠子类来实例化对象

FileInputStream构造方法:

  • public FileInputStream​(File file) throws FileNotFoundException

 

范例:读取数据

import java.io.*;

public class InputStreamDemo {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("D:"+File.separator+"hello"
                +File.separator+"demo.txt");//读取的文件路径
        InputStream input = new FileInputStream(file);
        byte[] data = new byte[1024];//用于存放读取出的内容的缓冲区
        int len = input.read(data);//读取的内容长度
        System.out.println("【"+new String(data,0,len)+"】");//输出缓冲区读取的内容
        input.close();
    }

}

 

4.字符输出流:Writer类

OutputStream数据输出的时候使用的是字节,而很多时候字符串的输出是比较方便的,所以在JDK1.1时推出字符输出流

类的基本定义:

  • public abstract class Writer extends Object implements Appendable, Closeable, Flushable

两个主要的输出方法:

  • 输出字符数组:public void write​(char[] cbuf)throws IOException
  • 输出字符串:public void write​(String str)throws IOException

 

范例:使用Writer输出

import java.io.*;

public class WriterDemo {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("D:"+File.separator+"hello"
                +File.separator+"demo.txt");         //1.指定要操作的文件路径
        if(!file.getParentFile().exists()) { //文件不存在
            file.getParentFile().mkdirs();   //创建父目录
        }
        Writer out = new FileWriter(file,true);
        String str = "goodbye world\r\n";
        out.write(str);
        out.append("hahahaha");//追加输出内容
        out.close();
    }

}

 

5.字符输入流:Reader类

类的基本定义:

  • public abstract class Reader extends Object implements Readable, Closeable

输入方法:

  • 读取单个字节数据,如果以及读取到底,返回-1:public abstract int read() throws IOException
  • 读取一组字节数据,返回的是读取的个数,如果没有则返回-1:public int read​(byte[] b) throws IOException
  • 读取一组字节数据的部分内容(只占数组的部分):public int read​(byte[] b,int off,int len) throws IOException

字符流读取的时候只能够按照数组的形式来实现处理操作,不能够直接按照字符串的形式

 

范例:实现数据读取

import java.io.*;

public class ReaderDemo {

    public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
        File file = new File("D:"+File.separator+"hello"
                +File.separator+"demo.txt");   
        if(file.exists()) {
            Reader in = new FileReader(file);
            char[] data = new char[1024];
            int len = in.read(data);
            System.out.println(new String(data,0,len));
            in.close();
        }
    }

}

 

6.字节流和字符流大的区别

    在使用OutputStream类输出的时候如果没有使用close()方法关闭输出流发现内容依然可以实现正常的输出
但是如果在使用Writer的时候没有使用close()方法关闭输出流,那么这个内容将无法进行输出,因为Writer使
用到缓冲区,当使用了close()方法的时候实际上会出现有强制刷新缓冲区的情况,所以这个时候会将内容进行
输出操作,所以此时如果在不关闭的情况下想要将全部内容输出可以使用flush()方法强制情况缓冲区

    字符流的优势在于处理中文数据上

 

7.转换流

    转换流就是可以实现字节流与字符流操作的功能转换,例如:进行输出的时候OutputStream需要将内容变
为字节数组后才可以输出,而Writer可以直接输出字符串,这一点是方便的,所以很多人就认为需要提供一种
转换的机制,来实现不同的流类型的转换操作,为此在java.io包里面提供有两个类:InputStreamReader
OutputStreamWriter。

类:           OutputStreamWriter                                                            InputStreamReader

定义:       public class OutputStreamWriter extends Writer                public class InputStreamReader extends Reader

构造方法:public OutputStreamWriter​(OutputStream out)                   public InputStreamReader​(InputStream in)

从构造方法可以看出,所谓的转换处理,就是将接收到的字节流对象向上转型变为字符流对象

 

范例:转换流

import java.io.*;

public class OutputStreamWriterDemo {

    public static void main(String[] args) throws Exception {
        // TODO Auto-generated method stub
        File file = new File("D:"+File.separator+"hello"
                +File.separator+"demo.txt");   
        if(!file.getParentFile().exists()) { //文件不存在
            file.getParentFile().mkdirs();   //创建父目录
        }
        OutputStream output = new FileOutputStream(file,true);
        Writer out = new OutputStreamWriter(output);
        out.write("i am back");
        out.close();
    }

}
posted @ 2020-01-18 20:52  幼儿猿班长  阅读(189)  评论(0编辑  收藏  举报