Java IO学习笔记(八):System类对IO的支持

来源:互联网 发布:知乎痴情叔被骂 编辑:程序博客网 时间:2024/06/09 00:10

System类的常量

System表示系统类,此类也对IO给予了一定的支持。

  • public static final PrintStream out  对应系统标准输出,一般是显示器
  • public static final PrintStream err   错误信息输出
  • public static final InputStream in   对应着标准输入,一般是键盘
又是由于历史遗留问题 全局变量没有大写~

System.out

使用System.out输出的时候就是将输出的位置定义在了显示器之中。

FileOutputStream是定位在文件里,而System.out是定位在屏幕上。

使用OutputStream完成屏幕上输出(PrintStream是OutputStream的子类

 1 import java.io.IOException; 2 import java.io.OutputStream; 3  4 public class Test26 { 5     public static void main(String[] args) throws IOException { 6         OutputStream out=System.out; 7         out.write("Hello World!".getBytes()); 8         out.close(); 9     }10 }
这其实就是多态的一种表现

 

System.err

System.err表示的错误的标准输出,如果程序中出现了错误的话,则直接使用System.err进行打印输出即可。

复制代码
 1 public class Test27 { 2     public static void main(String[] args) { 3         String str="Hello World"; 4         try{ 5         int a=Integer.parseInt(str); 6         }catch(Exception e){ 7             System.err.println(e); 8         } 9     }10 }
复制代码

但是有些人会问这跟System.out输出的结果完全一样的嘛,有什么区别啊?

 

System.out与System.err的区别

  • System.out和System.err都是PrintStream的实例化对象,而且通过实例代码可以发现,两者都可以输出错误信息,但是一般来讲System.out是将信息显示给用户看,是正常的信息显示,而System.err的信息正好相反是不希望用户看到,会直接在后台打印,是专门显示错误的
  • 一般来讲,如果要想输出错误信息的时候最好不要使用System.out而是直接使用System.err,这一点只能从其概念上划分。

System.in

System.in实际上是一个键盘的输入流,其本身是InputStream类型的对象。那么,此时就可以利用此方式完成从键盘读取数据的功能。

InputStream对应的是输入流,输入流的话肯定可以从指定位置上读取,之前使用的是FileInputStream是从文件中读取的

复制代码
 1 import java.io.IOException; 2 import java.io.InputStream; 3  4 public class Test28 { 5     public static void main(String[] args) throws IOException { 6         InputStream in=System.in; 7         byte[] b=new byte[1024]; 8         int len=in.read(b); 9         System.out.println(new String(b,0,len));10     }11 }
复制代码

如果不使用byte数组指定长度呢:

复制代码
 1 import java.io.IOException; 2 import java.io.InputStream; 3  4 public class Test29 { 5     public static void main(String[] args) throws IOException { 6         InputStream in=System.in; 7         StringBuilder buf=new StringBuilder(); 8         int temp=0; 9         while((temp=in.read())!=-1){10             char c=(char) temp;11             if(c=='\n')break;12             buf.append(c);13         }14         in.close();15         System.out.println(buf.toString());16     }17 }
复制代码

但以上代码还是有很大问题的,输入中文的话~,所以最好的方法还是一次性把数据都放在内存了,再一次性全部拿出来,要实现这个功能的话,要用到BufferedReader类,下回介绍~

 

输入输出重定向

System.out、System.err、System.in都有重定向功能,分别是setOut、setErr、setIn方法

System.out重定向

复制代码
 1 import java.io.File; 2 import java.io.FileNotFoundException; 3 import java.io.PrintStream; 4  5 public class Test30 { 6     public static void main(String[] args) throws FileNotFoundException { 7         File f = new File("d:" + File.separator+"test.txt"); 8         System.setOut(new PrintStream(f)); 9         String str="This is a test!";10         System.out.println(str);11     }12 }
复制代码

System.err重定向

复制代码
 1 import java.io.ByteArrayOutputStream; 2 import java.io.PrintStream; 3  4 public class Test31 { 5     public static void main(String[] args) { 6         ByteArrayOutputStream out=new ByteArrayOutputStream(); 7         System.setErr(new PrintStream(out)); 8         System.err.println("Test---------------"); 9         System.out.println(out.toString());10     }11 }
复制代码

一般不建议修改err的重定向,因为这些信息都不太希望用户看到

System.in重定向

复制代码
 1 import java.io.File; 2 import java.io.FileInputStream; 3 import java.io.IOException; 4 import java.io.InputStream; 5  6 public class Test32 { 7     public static void main(String[] args) throws IOException { 8         File f = new File("d:" + File.separator+"test.txt"); 9         System.setIn(new FileInputStream(f));10         InputStream in=System.in;11         byte[] b=new byte[1024];12         int len=in.read(b);13         in.close();14         System.out.println(new String(b,0,len));15     }16 }
复制代码


一般,使用最多的还是System.out的重定向~

原创粉丝点击