java学习之路---IO----System和BufferReader类

来源:互联网 发布:手机上网控制软件 编辑:程序博客网 时间:2024/04/29 23:07
      

1.System.out

          System.out是PriteStream的对象,在PriteStream中定义一系列的print()和println()

     public class PrintDemo1 {
     public static void main(String[] args) throws Exception{

          OutputStream out=System. out;//此时的输出时向屏幕输出的
          
          out.write( "hello world".getBytes());
          
          out.close();
          
          
     }

}

结果:

hello world


2.System.err输出错误信息
这个就不写了

3.System.in键盘输入流

public class PrintDemo3 {
     public static void main(String[] args) throws Exception{
              InputStream in=System. in;
              
               byte b[]=new byte[1024];
              
              System. out.println("请输入内容" );
              
               int len=in.read(b);
              
              in.close();
              System. out.println(new String(b,0,len));
     }

}

结果:
请输入内容
asdasd
asdasd

上面程序会如果长度过长就会超过byte[1024],就会出现不会完全输出的结果

那我们可以不要长度限制

          public class PrintDemo3 {
     public static void main(String[] args) throws Exception{
              InputStream in=System. in;
              
              StringBuffer buf= new StringBuffer();//用来接收数据
              
              System. out.println("请输入内容" );
              
               int temp=0;
              
               int len=0;
              
               while((temp=in.read())!=-1){
                         char c=(char )temp;
                         if(c=='\n' )
                        {
                              break;
                        }
                        buf.append(c);
              }
              
              
              in.close();
              System. out.println(new String(buf));
     }

}

结果:
请输入内容
我爱你
鎴戠埍浣�

有出现了乱码,那我们需要怎样做喃?

我们可以让字符串全部输入缓冲区,然后一次性输出,这样就可以避免代码的乱码了

               需要用到BufferReader类



buffReader类直接收字符流的实例

          public class BufferReaderDemo {
           public static void main(String[] args) throws Exception{
              BufferedReader buf= new BufferedReader(new InputStreamReader(System.in ));
              
              String str= null;
              
              System. out.println("请输入内容" );
              
              str=buf.readLine();
              buf.close();
              System. out.println(str);
          }

}
结果:
请输入内容
sadasdasdasdasdasd
sadasdasdasdasdasd

下面来做一些例子:

               加法操作:

     public class BufferReaderDemo1 {
           public static void main(String[] args)throws Exception {
              BufferedReader buf= null;
              buf= new BufferedReader(new InputStreamReader(System.in ));
              
              String str= null;
              
              System. out.println("请输入第一个数字" );
              
              str  =buf.readLine();
               int i=Integer.parseInt(str);
              
              System. out.println("请输入第二个数字" );
              
              str=buf.readLine();
              
               int j=Integer.parseInt(str);
              
              buf.close();
              
              System. out.println(i+"+" +j+"=" +(i+j));
          }
}
结果:
请输入第一个数字
2
请输入第二个数字
3
2+3=5




System类也可以改变System.in的输入流来源,也可以改变System.out和System.err的输出位置
               

public class SystemDemo1 {
           public static void main(String[] args)throws Exception {
              System. setOut(new PrintStream( new FileOutputStream(new File("f:"+File.separator +"b.txt" ))));
              
              System. out.println("like" );
              System. out.println("hello" );
          }

}
程序运行后,不会在屏幕上显示,而是在文件b.txt中

system.err最好不要输出重定向

          System.in,改变输入来源

public class SystemDemo2 {
           public static void main(String[] args) throws Exception{
              System. setIn(new FileInputStream( new File("f:"+File.separator +"a.txt" )));
              
              InputStream in=System. in;
               int len=0;
               byte b[]=new byte[1024];
              len=in.read(b);
              in.close();
              System. out.println(new String(b,0,len));
          }

}

结果:
hello like!!!


输入来源来自文件a.txt中