来源IT资料库:http://www.itzlk.com/io/362.jhtml
管道流
管道流主要可以进行两个线程之间的通信。
PipedOutputStream 管道输出流
PipedInputStream 管道输入流
验证管道流
- /**
- * 验证管道流
- * */
- import java.io.*;
- /**
- * 消息发送类
- * */
- class Send implements Runnable{
- private PipedOutputStream out=null;
- public Send() {
- out=new PipedOutputStream();
- }
- public PipedOutputStream getOut(){
- return this.out;
- }
- public void run(){
- String message="hello , Rollen";
- try{
- out.write(message.getBytes());
- }catch (Exception e) {
- e.printStackTrace();
- }try{
- out.close();
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- /**
- * 接受消息类
- * */
- class Recive implements Runnable{
- private PipedInputStream input=null;
- public Recive(){
- this.input=new PipedInputStream();
- }
- public PipedInputStream getInput(){
- return this.input;
- }
- public void run(){
- byte[] b=new byte[1000];
- int len=0;
- try{
- len=this.input.read(b);
- }catch (Exception e) {
- e.printStackTrace();
- }try{
- input.close();
- }catch (Exception e) {
- e.printStackTrace();
- }
- System.out.println("接受的内容为 "+(new String(b,0,len)));
- }
- }
- /**
- * 测试类
- * */
- class hello{
- public static void main(String[] args) throws IOException {
- Send send=new Send();
- Recive recive=new Recive();
- try{
- //管道连接
- send.getOut().connect(recive.getInput());
- }catch (Exception e) {
- e.printStackTrace();
- }
- new Thread(send).start();
- new Thread(recive).start();
- }
- }
【运行结果】:
接收的内容为 hello , Rollen
打印流
- /**
- * 使用PrintStream进行输出
- * */
- import java.io.*;
- class hello {
- public static void main(String[] args) throws IOException {
- PrintStream print = new PrintStream(new FileOutputStream(new File("d:"
- + File.separator + "hello.txt")));
- print.println(true);
- print.println("Rollen");
- print.close();
- }
- }
【运行结果】:
true
Rollen
当然也可以格式化输出
- /**
- * 使用PrintStream进行输出
- * 并进行格式化
- * */
- import java.io.*;
- class hello {
- public static void main(String[] args) throws IOException {
- PrintStream print = new PrintStream(new FileOutputStream(new File("d:"
- + File.separator + "hello.txt")));
- String name="Rollen";
- int age=20;
- print.printf("姓名:%s. 年龄:%d.",name,age);
- print.close();
- }
- }
【运行结果】:
姓名:Rollen. 年龄:20.
使用OutputStream向屏幕上输出内容
- /**
- * 使用OutputStream向屏幕上输出内容
- * */
- import java.io.*;
- class hello {
- public static void main(String[] args) throws IOException {
- OutputStream out=System.out;
- try{
- out.write("hello".getBytes());
- }catch (Exception e) {
- e.printStackTrace();
- }
- try{
- out.close();
- }catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
【运行结果】:hello
输入输出重定向
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.PrintStream;
- /**
- * 为System.out.println()重定向输出
- * */
- public class systemDemo{
- public static void main(String[] args){
- // 此刻直接输出到屏幕
- System.out.println("hello");
- File file = new File("d:" + File.separator + "hello.txt");
- try{
- System.setOut(new PrintStream(new FileOutputStream(file)));
- }catch(FileNotFoundException e){
- e.printStackTrace();
- }
- System.out.println("这些内容在文件中才能看到哦!");
- }
- }
【运行结果】:eclipse的控制台输出的是hello。然后当我们查看d盘下面的hello.txt文件的时候,会在里面看到:这些内容在文件中才能看到哦!
System.err重定向
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.PrintStream;
- /**
- * System.err重定向 这个例子也提示我们可以使用这种方法保存错误信息
- * */
- public class systemErr{
- public static void main(String[] args){
- File file = new File("d:" + File.separator + "hello.txt");
- System.err.println("这些在控制台输出");
- try{
- System.setErr(new PrintStream(new FileOutputStream(file)));
- }catch(FileNotFoundException e){
- e.printStackTrace();
- }
- System.err.println("这些在文件中才能看到哦!");
- }
- }
【运行结果】:你会在eclipse的控制台看到红色的输出:“这些在控制台输出”,然后在d盘下面的hello.txt中会看到:这些在文件中才能看到哦!
System.in重定向
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- /**
- * System.in重定向
- * */
- public class systemIn{
- public static void main(String[] args){
- File file = new File("d:" + File.separator + "hello.txt");
- if(!file.exists()){
- return;
- }else{
- try{
- System.setIn(new FileInputStream(file));
- }catch(FileNotFoundException e){
- e.printStackTrace();
- }
- byte[] bytes = new byte[1024];
- int len = 0;
- try{
- len = System.in.read(bytes);
- }catch(IOException e){
- e.printStackTrace();
- }
- System.out.println("读入的内容为:" + new String(bytes, 0, len));
- }
- }
- }
【运行结果】:前提是我的d盘下面的hello.txt中的内容是:“这些文件中的内容哦!”,然后运行程序,输出的结果为:读入的内容为:这些文件中的内容哦!