Java IO流——管道流与打印流

来源IT资料库:http://www.itzlk.com/io/362.jhtml


管道流 

管道流主要可以进行两个线程之间的通信。
PipedOutputStream 管道输出流
PipedInputStream 管道输入流

 

验证管道流

  1. /**
  2.  * 验证管道流
  3.  * */
  4. import java.io.*;
  5.  
  6. /**
  7.  * 消息发送类
  8.  * */
  9. class Send implements Runnable{
  10.     private PipedOutputStream out=null;
  11.     public Send() {
  12.         out=new PipedOutputStream();
  13.     }
  14.     public PipedOutputStream getOut(){
  15.         return this.out;
  16.     }
  17.     public void run(){
  18.         String message="hello , Rollen";
  19.         try{
  20.             out.write(message.getBytes());
  21.         }catch (Exception e) {
  22.             e.printStackTrace();
  23.         }try{
  24.             out.close();
  25.         }catch (Exception e) {
  26.             e.printStackTrace();
  27.         }
  28.     }
  29. }
  30.  
  31. /**
  32.  * 接受消息类
  33.  * */
  34. class Recive implements Runnable{
  35.     private PipedInputStream input=null;
  36.     public Recive(){
  37.         this.input=new PipedInputStream();
  38.     }
  39.     public PipedInputStream getInput(){
  40.         return this.input;
  41.     }
  42.     public void run(){
  43.         byte[] b=new byte[1000];
  44.         int len=0;
  45.         try{
  46.             len=this.input.read(b);
  47.         }catch (Exception e) {
  48.             e.printStackTrace();
  49.         }try{
  50.             input.close();
  51.         }catch (Exception e) {
  52.             e.printStackTrace();
  53.         }
  54.         System.out.println("接受的内容为 "+(new String(b,0,len)));
  55.     }
  56. }
  57. /**
  58.  * 测试类
  59.  * */
  60. class hello{
  61.     public static void main(String[] args) throws IOException {
  62.         Send send=new Send();
  63.         Recive recive=new Recive();
  64.         try{
  65. //管道连接
  66.             send.getOut().connect(recive.getInput());
  67.         }catch (Exception e) {
  68.             e.printStackTrace();
  69.         }
  70.         new Thread(send).start();
  71.         new Thread(recive).start();
  72.     }
  73. }

【运行结果】:
接收的内容为 hello , Rollen

 


打印流

  1. /**
  2.  * 使用PrintStream进行输出
  3.  * */
  4. import java.io.*;
  5.  
  6. class hello {
  7.     public static void main(String[] args) throws IOException {
  8.         PrintStream print = new PrintStream(new FileOutputStream(new File("d:"
  9.                 + File.separator + "hello.txt")));
  10.         print.println(true);
  11.         print.println("Rollen");
  12.         print.close();
  13.     }
  14. }

【运行结果】:
true
Rollen

 

当然也可以格式化输出

  1. /**
  2.  * 使用PrintStream进行输出
  3.  * 并进行格式化
  4.  * */
  5. import java.io.*;
  6. class hello {
  7.     public static void main(String[] args) throws IOException {
  8.         PrintStream print = new PrintStream(new FileOutputStream(new File("d:"
  9.                 + File.separator + "hello.txt")));
  10.         String name="Rollen";
  11.         int age=20;
  12.         print.printf("姓名:%s. 年龄:%d.",name,age);
  13.         print.close();
  14.     }
  15. }

【运行结果】:

姓名:Rollen. 年龄:20.

使用OutputStream向屏幕上输出内容

  1. /**
  2.  * 使用OutputStream向屏幕上输出内容
  3.  * */
  4. import java.io.*;
  5. class hello {
  6.     public static void main(String[] args) throws IOException {
  7.         OutputStream out=System.out;
  8.         try{
  9.             out.write("hello".getBytes());
  10.         }catch (Exception e) {
  11.             e.printStackTrace();
  12.         }
  13.         try{
  14.             out.close();
  15.         }catch (Exception e) {
  16.             e.printStackTrace();
  17.         }
  18.     }
  19. }

【运行结果】:hello

 

 

输入输出重定向

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.PrintStream;
  5.  
  6. /**
  7.  * 为System.out.println()重定向输出
  8.  * */
  9. public class systemDemo{
  10.     public static void main(String[] args){
  11.         // 此刻直接输出到屏幕
  12.         System.out.println("hello");
  13.         File file = new File("d:" + File.separator + "hello.txt");
  14.         try{
  15.             System.setOut(new PrintStream(new FileOutputStream(file)));
  16.         }catch(FileNotFoundException e){
  17.             e.printStackTrace();
  18.         }
  19.         System.out.println("这些内容在文件中才能看到哦!");
  20.     }
  21. }

【运行结果】:eclipse的控制台输出的是hello。然后当我们查看d盘下面的hello.txt文件的时候,会在里面看到:这些内容在文件中才能看到哦!

 

 

System.err重定向

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.io.FileOutputStream;
  4. import java.io.PrintStream;
  5.  
  6. /**
  7.  * System.err重定向 这个例子也提示我们可以使用这种方法保存错误信息
  8.  * */
  9. public class systemErr{
  10.     public static void main(String[] args){
  11.         File file = new File("d:" + File.separator + "hello.txt");
  12.         System.err.println("这些在控制台输出");
  13.         try{
  14.             System.setErr(new PrintStream(new FileOutputStream(file)));
  15.         }catch(FileNotFoundException e){
  16.             e.printStackTrace();
  17.         }
  18.         System.err.println("这些在文件中才能看到哦!");
  19.     }
  20. }

【运行结果】:你会在eclipse的控制台看到红色的输出:“这些在控制台输出”,然后在d盘下面的hello.txt中会看到:这些在文件中才能看到哦!

 

 

System.in重定向

  1. import java.io.File;
  2. import java.io.FileInputStream;
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5.  
  6. /**
  7.  * System.in重定向
  8.  * */
  9. public class systemIn{
  10.     public static void main(String[] args){
  11.         File file = new File("d:" + File.separator + "hello.txt");
  12.         if(!file.exists()){
  13.             return;
  14.         }else{
  15.             try{
  16.                 System.setIn(new FileInputStream(file));
  17.             }catch(FileNotFoundException e){
  18.                 e.printStackTrace();
  19.             }
  20.             byte[] bytes = new byte[1024];
  21.             int len = 0;
  22.             try{
  23.                 len = System.in.read(bytes);
  24.             }catch(IOException e){
  25.                 e.printStackTrace();
  26.             }
  27.             System.out.println("读入的内容为:" + new String(bytes, 0, len));
  28.         }
  29.     }
  30. }

【运行结果】:前提是我的d盘下面的hello.txt中的内容是:“这些文件中的内容哦!”,然后运行程序,输出的结果为:读入的内容为:这些文件中的内容哦!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值