上季内容回顾:
字节流:InputStream、OutputStream
字符流:Reader、Writer
字符流:Reader、Writer
上季练习题:
既然是文件的拷贝,肯定要使用字节流,所以建立一个字节的输入流再建立一个字节的输出流,边读边写。
看下效果:
IO操作就是研究五个类一个接口
· File类
· OutputStream、InputStream
· Reader、Writer
· 对象序列化
· File类
· OutputStream、InputStream
· Reader、Writer
· 对象序列化
管道流
了解:管道流是管两个线程间通讯使的。
public void connect(PipedOutputStream src) throws IOException
把输出与输入连接在一起。
import java.io.*;
//设置两个线程,一个发送另一个接收
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();
}
}
//设置两个线程,一个发送另一个接收
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
内存的输入、输出流
之前讲解的很多内容都是把内容直接输出到文件中去了,那么现在把内容输入到内存中,直接从内存中输出。
看下有没变成大写哈~
应该按以下的方式理解:
· ByteArrayInputStream():理解为内存要读取
· ByteArrayOutputStream():从内存中输出
· ByteArrayInputStream():理解为内存要读取
· ByteArrayOutputStream():从内存中输出
输入输出方法
System.out:
|- PrintStream是一个打印流,表示提供了更好的打印操作。
|- PrintStream是一个打印流,表示提供了更好的打印操作。
PrintStream有那些好处呢?
PrintStream是OutputStream的子类
PrintStream是OutputStream的子类
实际上在IO中体现了一种设计模式 ——
装饰模式。
· 发现在OutputStream中的功能虽然可以输出,但是输出的过程不太好操作,因为所有的内容都要向byte数组转换。
· 发现在OutputStream中的功能虽然可以输出,但是输出的过程不太好操作,因为所有的内容都要向byte数组转换。
PrintStream的基本原理
PrintStream 中提供了比OutputStream 中更多的打印功能,可以输出任意的数据,也可以对输出的数据进行换行的处理。
看下效果:在屏幕上换行是\n就可以了,在文件中换行是\r\n。
现在既然已经知道了PrintStream是OutputStream的对象,那么问?是否可以通过OutputStream完成向屏幕上输出的功能呢?
可以使用对象多态性哈~~~
PrintStream 是一个字节的打印流,与它类似的还有一个PrintWirter字符的打印流,建议使用字节打印流是比较方便。
总结
1、System.out与OutputStream的关系
2、PrintStream实际上是JAVA装饰模式的应用,具有比父类提供更多更方便的功能
1、System.out与OutputStream的关系
2、PrintStream实际上是JAVA装饰模式的应用,具有比父类提供更多更方便的功能
####################################################
本文转自redking51CTO博客,原文链接:http://blog.51cto.com/redking/133816
,如需转载请自行联系原作者