java中的流的操作

今天在练习java中的IO操作时,发现了一个有趣的问题,来给大家来分享一下。

这是问题程序:

import java.io.* ;
/*
通过子类对象实
以完成不同的功能
*/
public class TestIO8
{
 public static void main(String args[])
 {
  OutWindow();
  try
  {
   WriteFile();
  }
  catch(Exception ex){}
  
 }
 
 public static void OutWindow()
 {
  try
   {
    OutputStream out = null;
    out = System.out;
  
    String str = "Welcome here!";
    out.write(str.getBytes());
  
    out.close();
   }
   catch(Exception ex)
   {
   }
  Input();
 
 }
 
 public static void WriteFile()
  {
   
    File f = new File("f://lxh.txt") ;
    // 使用PrintWriter
    // PrintWriter out = new PrintWriter(new FileWriter(f)) ;
    PrintWriter out = new PrintWriter(System.out) ;
    // 具备了向文件中打印数据的能力
    out.println(true) ;
    out.println(30) ;
    out.println("HELLO MLDN") ;
    out.close() ;

   
  }

 public static void Input()
  {
   try
   {
    InputStream in = null;
    in = System.in;
  
    byte [] b = new byte[1024];

    System.out.println("请输入类容");
    int length = in.read(b);
  
    in.close();
    System.out.println("输入的类容是:" + new String(b,0,length));
   }
  catch(Exception ex)
   {
    System.out.println(ex);
   }
   
  } 

}发现改程序制执行了OutWindow方法。没办法我只好将程序该了一下。

mport java.io.* ;
/*
通过子类对象实
以完成不同的功能
*/
public class TestIO9
{
 public static void main(String args[])
 {
  Input();
 
 } 
 
 public static void Input()
  {
   try
   {
    InputStream in = null;
    in = System.in;
  
    byte [] b = new byte[1024];

    System.out.println("请输入类容");
    int length = in.read(b);
  
    //in.close();先前我在这里写了这段代码然后在程序运行的时候给我抛出了这么一个异常:
   //Stream closed哦原来是这里
    System.out.println("输入的类容是:" + new String(b,0,length));
   }
  catch(Exception ex)
   {
    System.out.println(ex);
   }
  fun();
   
  }
 public static void fun()
  {
   BufferedReader buffer = null;
   buffer = new BufferedReader(new InputStreamReader(System.in));
   
   String str = null;
   try
   {
    System.out.println("输入内容:");
    str = buffer.readLine();
   }
   catch(Exception ex)
   {
    System.out.println(ex);
   }
   System.out.println("输出的内容是: " + str);
  } 

}

可是我还是不太理解按道理我这里  ( buffer = new BufferedReader(new InputStreamReader(System.in));)已经从新开通流了啊可为什么还要报出这么一个异常呢。Stream closed于是我上网找了一下看有没有类似的异常。可是没有类似的问题。
 

是不是这段程序只有一个流啊?

还请大家帮忙解答一下。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值