本章目标
掌握System对IO的三种支持:
——System.out
——System.err
——System.in
掌握System.out及System.err的区别
掌握输入、输出重定向
System类的常量
System表示系统类,此类在之前讲解JAVA常用类库的时候就已经为读者介绍过了,实际上在java中System类也对IO给予了一定的支持。
System.out
System.out是PrintStream的对象,在PrintStream中定义了一系列的print()和println()方法,所以之前使用的“System.out.print()”或“System.out.println()”语句调用的实际上就是PrintStream类的方法。
使用OutputStream向屏幕上输出
import java.io.IOException;
import java.io.OutputStream;
public class SystemDemo01{
public static void main(String[] args) {
OutputStream out = System.out;//此时的输出流是向屏幕上输出
try{
out.write("hello world!!!".getBytes());//向屏幕上输出
}catch(IOException e){
e.printStackTrace();
}
try{
out.close();//关闭输出流
}catch(IOException e){
e.printStackTrace();
}
}
/* 结果:
* hello world!!!
* */
}
System.err
System.err表示的是错误信息输出,如果程序出现错误,则可以直接使用System.err进行打印。
打印错误信息
public class SystemDemo02{
public static void main(String[] args) {
String str = "hello";//声明一个非数字的字符串
try{
System.out.println(Integer.parseInt(str));
}catch(Exception e){
System.err.println(e);
}
}
/* 结果:
* java.lang.NumberFormatException: For input string: "hello"
* */
}
System.out和System.err的区别
System.out和System.err都是PrintStream的实例化对象,而且通过实例代码可以发现,两者都可以输出错误信息,但是一般来讲System.out是将信息显示给用户看,是正常的信息显示,而System.err的信息正好相反是不希望用户看到的,会直接在后台打印,是专门显示错误的
一般来讲,如果要想输出错误信息的时候最好不要使用System.out而是直接使用System.err,这一点只能从其概念上划分。
System.in
System.in实际上是一个键盘的输入流,其本身是InputStream类型的对象。那么,此时就可以利用此方式完成从键盘读取数据的功能。
从键盘上读取数据
import java.io.InputStream;
public class SystemDemo04{
public static void main(String[] args) throws Exception {//所有异常抛出
InputStream input = System.in;//从键盘接收数据
byte b[] = new byte[1024];//开辟空间,接收数据
System.out.print("请输入内容:");//信息提示
int len = input.read(b);//接收数据
System.out.print("输入的内容为:"+new String(b,0,len));
input.close();//关闭输入流
}
/* 结果:
* 请输入内容:chaoyi
* 输入的内容为:chaoyi
* */
}
输入的问题
那么,如果此时不指定byte数组长度呢?是否可以完成输入的要求呢?
不指定大小
import java.io.InputStream;
public class SystemDemo05{
public static void main(String[] args) throws Exception {//所有异常抛出
InputStream input = System.in;//从键盘接收数据
StringBuffer buf = new StringBuffer();//声明 StringBuffer 用于接收数据
System.out.print("请输入内容:");
int temp = 0;
while((temp = input.read()) != -1){//循环接收
char c = (char)temp;//将数据变为字符
if(c =='\n'){
break;//退出循环,按 Enter 键表示输入完成
}
buf.append(c);//追加数据
}
System.out.println("输入的内容为:"+buf);
input.close();//关闭输入流
}
/* 结果:
* 请输入内容:www.baidu
* 输入的内容为:www.baidu
* 请输入内容:百度一下
* 输入的内容为:°???????
* */
}
最好的输入方式
最好的输入方式是将全部输入的数据暂时放到一块内存之中,之后一次性的从内存中读取出数据,这样所有数据只读了一次,则不会造成乱码,而且也不会受长度的限制。
输入、输出重定向
从之前的操作中读者已经了解了System.out、System.err、System.in三个常量的作用,但是通过System类也可以改变System.in的输入流来源,以及System.out和System.err两个输出流的输出位置。
为System.out输出重定向
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class SystemDemo06{
public static void main(String[] args) throws Exception {//所有异常抛出
//System.out 输出重定向
System.setOut(new PrintStream(new FileOutputStream("d:"+File.separator+"test.txt")));
//输出时,不再向控制台上输出,而是向指定的重定向位置输出
System.out.println("www.baidu.com");
System.out.println("百度一下");
}
/* 结果:
* www.baidu.com
* 百度一下
* */
}
为用户保存错误信息
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
public class SystemDemo07{
public static void main(String[] args) {
String str = "hello";//声明一个非数字的字符串
try{
System.out.println(Integer.parseInt(str));
}catch(Exception e){
try{
//输出重定位
System.setOut(new PrintStream(new FileOutputStream("d:"+File.separator+"err.log")));
}catch(FileNotFoundException e1){
e1.printStackTrace();
}
//输出错误,保存到文件中
System.out.println(e);
}
}
/* 结果:
* java.lang.NumberFormatException: For input string: "hello"
* */
}
为System.err输出重定向
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
public class SystemDemo08{
public static void main(String[] args) throws Exception {//所有异常抛出
ByteArrayOutputStream bos = null;//定义内存输出流
bos = new ByteArrayOutputStream();//实例化内存输出流
System.setErr(new PrintStream(bos));//System.err 输出重定向
System.err.println("www.baidu.com");//错误输出时,不再向屏幕上输出
System.err.println("百度一下");//而是向内存的位置输出
System.out.println(bos);//打印错误信息
}
/* 结果:
* www.baidu.com
* 百度一下
* */
}
设置System.in的输入重定向
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
public class SystemDemo09{
public static void main(String[] args) throws Exception {//所有异常抛出
System.setIn(new FileInputStream("d:"+File.separator+"demo.txt"));//设置输入重定向
InputStream input = System.in;//从文件中接收数据
byte b[] = new byte[1024];//开辟空间,接收数据
int len = input.read(b);//接收数据
System.out.println("输入的内容为:"+new String(b,0,len));
input.close();//关闭输入流
}
/* 结果:
* 输入的内容为:www.baidu.com 百度一下
* */