字符流:Reader、Writer
· File类
· OutputStream、InputStream
· Reader、Writer
· 对象序列化
//设置两个线程,一个发送另一个接收
class Send implements Runnable{
private PipedOutputStream out = null;
//在构造方法中实例化此对象
public Send(){
this.out = new PipedOutputStream();
}
public void run(){
//启动线程,启动线程时直接发送
String str = "Hello World~~~";
try{
this.out.write(str.getBytes());
}catch (Exception e){}
}
//必须把管道输出流返回去
public PipedOutputStream getOut(){
return this.out;
}
}
class Receive implements Runnable{
private PipedInputStream input = null;
public Receive(){
this.input = new PipedInputStream();
}
public void run(){
//启动线程,接收内容
byte b[] = new byte[1024];
int len = 0;
try{
len = this.input.read(b);
this.input.close();
System.out.println(new String(b,0,len));
}catch (Exception e){}
}
public PipedInputStream getInput(){
return this.input;
}
}
public class IODemo01{
public static void main(String args[]){
Send s = new Send();
Receive r = new Receive();
//要把输出连接到输入上去哈~
try{
r.getInput().connect(s.getOut());
}catch (Exception e){}
new Thread(s).start();
new Thread(r).start();
}
}
· ByteArrayInputStream():理解为内存要读取
· ByteArrayOutputStream():从内存中输出
|- PrintStream是一个打印流,表示提供了更好的打印操作。
PrintStream是OutputStream的子类
· 发现在OutputStream中的功能虽然可以输出,但是输出的过程不太好操作,因为所有的内容都要向byte数组转换。
1、System.out与OutputStream的关系
2、PrintStream实际上是JAVA装饰模式的应用,具有比父类提供更多更方便的功能