System 类中存在三个常量:
No | 常量 | 描述 |
1 | public static finnal PrintStream out | 表示的是一个标准的输出,输出的位置是显示器 |
2 | public static final PrintStream err | 表示错误,错误的输出 |
3 | public static final InputStream in | 表示的是键盘的输入,标准输入 |
System.out
System.out是PrintStream 的实例,常用的方法就是向屏幕上打印信息,当然如果使用System.out的话也可以直接为OutputStream实例化
package org.systemiddemo;
import java.io.IOException;
import java.io.OutputStream;
public class SystemIoDemo01 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
OutputStream output = System.out; //此时具备了向屏幕输出的能力
String str = "Hello";
output.write(str.getBytes()); //输出内容
output.close();
}
}
System.err
System.err 表示错误的输出
package org.systemiddemo;
public class SystemIoDemo02 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
try{
Integer.parseInt("hello");
}catch(Exception e){
System.out.println(e);
System.err.println(e);
}
}
}
从代码本身观察不出任何的问题,只有在eclipse 下才会出现红色的字体。实际上对于以上的两个常量要想区分区别,只能从概念上讲:
System.out 一般的信息是愿意展示给用户看的
System.err 一般的信息是不愿意展示给用户看的
System.in
System.in 实际上表示的就是一个键盘的输入,使用此操作,可以完成键盘输入数据的功能
package org.systemiddemo;
import java.io.IOException;
import java.io.InputStream;
public class SystemIoDemo03 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream input = System.in; //准备键盘输入数据
System.out.print("输入数字:");
byte b[] = new byte[20]; //开辟空间接收内容
int len input.read(b); //接收内容
System.out.println("输出的是:"+new String(b,0,len));
}
}
此时已经实现了键盘输入 的功能,但是此程序中在使用时会存在长度的限制。而且输入中文的时候也会存在问题,那么此时就可以通过另外一种方式,不指定大小,边读边判断是否结束
package org.systemiddemo;
import java.io.IOException;
import java.io.InputStream;
public class SystemIoDemo04 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
InputStream input = System.in; //准备键盘输入数据
System.out.print("请输入");
int temp = 0 ; //接收内容
StringBuffer str = new StringBuffer();
while ((temp = input.read()) != -1){
char c = (char)temp; //转型
if (c == '\n'){ //判断是否是回车
break; //退出循环
}
str.append(c);
}
System.out.print(str.toString());
}
}
此时,数据读取的时候没有长度限制了。
但是在输入中文的时候无法正确的读取了,因为每次读的是一个字节,应该按照整体读取,那么如果要想完成更好的读取操作,则只能使用后续的BufferedReader类完成
输出、输入重定向
System.out、System.err 都有固定的输出目标不,都是屏幕
System.in 有固定的输入目标,都是键盘
但是在System 类中提供了一系列的输入输出重定向的方法,可以改变System.out、System.err、System.in 的输入输出位置
System.out 重定向:public static void setOut(PrintStream out)
System.err 重定向:public static void setErr(PrintStream err)
System.in 重定向:public static void setIn(InputStream in)
范例:验证输出重定向
package org.systemiddemo;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
public class SystemIoDemo05 {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
File file = new File("D:"+File.separator+"demo.txt");
System.setOut(new PrintStream(new FileOutputStream(file)));
System.out.println("错误输出");
}
}
提示:
之前曾经说过 System.err 是不希望用户看到的,而System.out是希望用户看到的,所以在开发中不建议改变System.err的输出位置,而只建议修改System.out的输出位置
package org.systemiddemo;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
public class SystemIoDemo06 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("D:"+File.separator+"demo.txt");
System.setErr(new PrintStream(new FileOutputStream(file)));
try{
Integer.parseInt("hello");
}catch(Exception e){
System.err.println("错误");
}
}
}
范例:输入重定向
package org.systemiddemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
public class SystemIoDemo07 {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("D:"+File.separator+"demo.txt");
System.setIn(new FileInputStream(file));
InputStream input = System.in;
byte[] b = new byte[20]; //开辟空间接收内容
int len = 0;
len = input.read(b); //接收内容
System.out.println(new String(b,0,len));
}
}
一般System.in 的操作最好不要去