Java IO(三)--字节流基本使用

I/O流分类:

InputStream和Read的子类都有read(),用来读取单个字节或字节数组

OutputStream和write的子类都有write(),用来写入单个字节或字节数组

一般都是通过装饰器Decorator模式叠加多个对象提供所期望的功能。创建单一的流,却需要创建多个对象

InputStream:

InputStream为字节输入流,一般都是通过其子类实现功能,是所有字节输入流的基类

public abstract class InputStream implements Closeable {
    private static final int MAX_SKIP_BUFFER_SIZE = 2048;

    public abstract int read() throws IOException;	//读取1byte的数据,返回int类型。若返回值=-1说明没有读取到任何字节

    public int read(byte[] var1) throws IOException {}	//读取var1.length个字节的数据放到var1数组中,返回值是读取的字节数

    public int read(byte[] var1, int off, int len) throws IOException {}	//从输入流中最多读取len个字节的数据,存放到偏移量为off的var1数组中。

    public long skip(long var1) throws IOException {} //忽略输入流中的n个字节,返回值是实际忽略的字节数, 跳过一些字节来读取 
	
	
    public int available() throws IOException {}	//返回输入流中可以读取的字节数。必须是InputStream的子类调用才行,本身调用返回0

    public void close() throws IOException {}	//每次读取结束,都要关闭输入流并释放与流相关的资源

}

OutputStream:

OutputStream为字节输出流,一般都是通过其子类实现功能,是所有字节输出流的基类

public abstract class OutputStream implements Closeable, Flushable {
    public OutputStream() {}

    public abstract void write(int var1) throws IOException;	//先将int转换为byte类型,把低字节写入到输出流中

    public void write(byte[] var1) throws IOException {}	//将数组var1中的字节写到输出流

    public void write(byte[] var1, int off, int len) throws IOException {}	//将数组var1的字节从偏移量off开始的len个字节写到输出流

    public void flush() throws IOException { }	//将数据缓冲区中数据全部输出,并清空缓冲区

    public void close() throws IOException { }	//每次写入结束,都要关闭输出流并释放与流相关的资源
}

FileInputStream和FileOutputStream:

public static void main(String[] args) throws IOException {
	String pathName = "D:" + File.separator + "a.txt";
	File file = new File(pathName);
	OutputStream outputStream = new FileOutputStream(file);
	outputStream.write("abc1".getBytes());
	outputStream.close();
	InputStream inputStream = new FileInputStream(file);
	byte[] bytes = new byte[1024];
	int length = inputStream.read(bytes);
	String s = new String(bytes, 0, length);
	System.out.println(s);
	inputStream.close();
}

输出结果:

我是好人追加内容

FileOutputStream用来从内存区读数据到文件,如果文件存在,会把内容覆盖,如果不存在,创建文件,然后把数据写入

FileInputStream用来从文件读数据到内存区

ByteArrayInputStream和ByteArrayOutputStream:

ByteArrayOutputStream创建的时候,程序内部创建一个byte[]类型的缓冲区,然后向数组中写入或读出byte型数据。

public static void main(String[] args) throws IOException {
	OutputStream outputStream = new ByteArrayOutputStream();
	outputStream.write("abc".getBytes());
	byte[] bytes = ((ByteArrayOutputStream) outputStream).toByteArray();
	outputStream.close();
	InputStream inputStream = new ByteArrayInputStream(bytes);
	byte[] bytes1 = new byte[1024];
	int len;
	while ((len = inputStream.read(bytes1)) != -1) {
		System.out.println(new String(bytes1, 0, len));
	}
}

输出结果:abc

可以从得到byte[]缓冲区,然后读取数据,可以向缓冲区多次写入数据,然后集中一次读取

BufferedInputStream、BufferedInputStream:

public static void main(String[] args) throws IOException {
	String pathName = "D:" + File.separator + "a.txt";
	File file = new File(pathName);

	BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(file));
	BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(new File("D:" + File.separator + "b.txt")));

	byte[] bytes = new byte[4];
	while (inputStream.read(bytes) != -1) {
		System.out.println(new String(bytes));
		outputStream.write(bytes);
	}
	inputStream.close();
	outputStream.close();
}

在文件流外面包装一层缓冲流读取函数,先从缓冲区读取数据

PS:在写入缓冲区结束之后,一定要调用flush()或者close(),否则数据一致存在缓冲区,而没有写出

PipedInputStream、PipedOutputStream:

  管道流主要用于连接两个线程的数据通信.通过连接而产生一个通信管道,PipedOutputStream向管道中写入数据,PipedInputStream从管

道中读取数据。管道输出流和管道输入流执行时不能互相阻塞,所以一般要开启独立线程分别执行

public class Sender implements Runnable{
    private PipedOutputStream outputStream = new PipedOutputStream();
    public PipedOutputStream outputStream() {
        return this.outputStream;
    }
    @Override
    public void run() {
        try {
            outputStream.write("message".getBytes());
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class Receiver implements Runnable{
    private PipedInputStream inputStream = new PipedInputStream();
    public PipedInputStream inputStream() {
        return this.inputStream;
    }

    @Override
    public void run() {
        byte[] bytes = new byte[1024];
        try {
            int len = inputStream.read(bytes);
            if (len != -1) {
                System.out.println("成功接收:" + new String(bytes));
            }
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
public class Test {
    public static void main(String[] args) throws IOException {
        Sender sender = new Sender();
        Receiver receiver = new Receiver();
        PipedOutputStream outputStream = sender.outputStream();
        PipedInputStream inputStream = receiver.inputStream();
        inputStream.connect(outputStream);
        new Thread(sender).start();
        new Thread(receiver).start();
    }
}

输出结果:

成功接收:message                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         

ObjectInputStream、ObjectOutputStream:

ObjectOutputStream可以把对象直接存入到文件中,然后利用ObjectInputStream读取文件还原成对象

@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class Student implements Serializable{

    private int id;
    private String name;
    private int sex;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}
public static void main(String[] args) throws Exception {
	String pathName = "D:" + File.separator + "a.txt";
	File file = new File(pathName);
	ObjectOutputStream outputStream = new ObjectOutputStream(new FileOutputStream(file));
	Student student = new Student(1001, "sam", 1);
	outputStream.writeObject(student);
	outputStream.close();

	ObjectInputStream inputStream = new ObjectInputStream(new FileInputStream(file));
	Student student1 = (Student)inputStream.readObject();
	System.out.println(student1);
}

PS:保存的对象必须实现了Serializable接口,否则出现异常:java.io.NotSerializableException: com.it.entity.Student

posted @ 2019-06-19 11:29  Diamond-Shine  阅读(496)  评论(0编辑  收藏  举报