Java IO(四)

  在文件操作流中,输入输出的目标都是文件,但是有时候,我们并不需要写入文件,只是需要中转一下而已,这样就会显得很麻烦,所以我们就可以使用内存操作流。在内存操作流中,输入输出目标都是内存。

内存输出流:ByteArrayOutputStream

内存输入流:ByteArrayInputStream

package com.fuwh.stream;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

public class ByteArrarOutputInputStreamTest01{

    public static void main(String[] args) throws Exception{
        //想内存中写入内容,然后读取
        String message="this is 内存操作流!";
        InputStream is=new ByteArrayInputStream(message.getBytes());    //将message保存在内存输入流中
        OutputStream os=new ByteArrayOutputStream();    //定义一个内存输出流
        int temp=0;
        while((temp=is.read())!=-1){        //从内存输入流中读取一个字节的内容
            char c=(char) temp;
            os.write(Character.toUpperCase(c));
        }
        System.out.println(os.toString());
    }
}
View Code

PipedOutputStream管道输入流

PipedInputStream管道输出流

  管道流表示的是两个进程之间的通信。需要将两个管道进行连接。

实例

package com.fuwh.stream;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

//定义一个向管道写入的线程类
class Send implements Runnable {
    
    private PipedOutputStream output=null;
    
    public Send(){
        this.output=new PipedOutputStream();
    }
    
    public PipedOutputStream getPipedOutputStream(){
        return this.output;
    }
    @Override
    public void run() {
        String sendMessage="what's the 操蛋!!!";
        try {
            output.write(sendMessage.getBytes());
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        try {
            this.output.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

//定义一个从管道读入的线程类
class Receive implements Runnable{

    private PipedInputStream input;
    
    public Receive(){
        this.input=new PipedInputStream();
    }
    
    public PipedInputStream getPipedInputStream(){
        return this.input;
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        byte b[]=new byte[1024];
        int length=0;
        try {
            length=this.input.read(b);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            this.input.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println(new String(b,0,length));
    }
    
}

public class PipedStreamTest01 {
    public static void main(String[] args) throws IOException {
        Send send=new Send();
        Receive receive=new Receive();
        send.getPipedOutputStream().connect(receive.getPipedInputStream());
        new Thread(send).start();    //启动线程
        new Thread(receive).start();    //启动线程
    }
}
View Code

管道字符输出流PipedWriter

管道字符输入流PipedReader

实例

package com.fuwh.stream;

import java.io.IOException;
import java.io.PipedReader;
import java.io.PipedWriter;



//定义一个写入管道的线程类
class Out implements Runnable{
    
    private PipedWriter out;
    
    public Out(){
        this.out=new PipedWriter();
    }
    
    public PipedWriter getPipedWriter(){
        return this.out;
    }
    
    @Override
    public void run() {
        // TODO Auto-generated method stub
        String writerMessage="in 苦逼 find 乐趣!!!";
        try {
            this.out.write(writerMessage);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        try {
            this.out.close();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}

//定义一个读取管道中内容的线程类
class In implements Runnable{
    
    private PipedReader in;
    
    public In(){
        this.in=new PipedReader();
    }
    
    public PipedReader getPipedReader(){
        return this.in;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        char[] message=new char[1024];
        int length=0;
        try {
            length=this.in.read(message);
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        System.out.println(new String(message,0,length));
    }
    
}


public class PipedReaderWriterTest01 {
    public static void main(String[] args) throws IOException {
        Out out=new Out();
        In in=new In();
        out.getPipedWriter().connect(in.getPipedReader());
        new Thread(out).start();
        new Thread(in).start();
    }
}
View Code

对象输出流:ObjectOutputStream

对象输入流:ObjectInputStream

对象流实现了对象的传输,但是只有实现了Serializable接口或Externalizable接口的类才能被传输。该类的构造方法如下:

  ·ObjectOutputStream()

  ·ObjectOutputStream(OutputStream out)

从第二个构造方法可以看出,该类根据实例化的方式不同,可以想不同的地方写入对象。

实例

package com.fuwh.stream;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

class Student implements Serializable{
    private static final long serialVersionUID = 1L;
    private String name;
    private int age;
    
    public Student(){}
    
    public Student(String name,int age){
        this.name=name;
        this.age=age;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student [name=" + name + ", age=" + age + "]";
    }
    
}
public class FileObjectStreamTest01 {

    public static void main(String[] args) throws Exception {
        //向文件中写入对象
        Student s=new Student("刘备",33);
        ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("object.txt")));
        oos.writeObject(s);
        oos.close();
        //从文件中读取对象
        ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File("object.txt")));
        Student newStudent=(Student)ois.readObject();
        System.out.println(newStudent);
        ois.close();
    }
}
View Code

原文地址:https://www.cnblogs.com/zerotomax/p/6486253.html