现在的位置: 首页 > 综合 > 正文

System类对IO的支持

2013年04月30日 ⁄ 综合 ⁄ 共 2642字 ⁄ 字号 评论关闭

System类对IO的支持


System类中存在着三个常量:

No.         常量                                           描述                                         
  public  static finalPrintStream out
   
表示的是一个标准的输出,输出的位置是显示器   
  public static final PrintStream err 表示错误,错误的输出
  public static final InputStream in 表示的是键盘的输入,标准输入

1、System.out

System.out是PrintStream的实例,常用的方法就是向屏幕上打印信息,当然如果使用System.out的话也可以直接为OutputStream实例化。

package com.demo.io;

import java.io.IOException;
import java.io.OutputStream;

public class SystemOutDemo {
	public static void main(String[] args) throws IOException {
		//此时具备了向屏幕输出的能力
		OutputStream outputStream = System.out;
		//输出内容
		outputStream.write("hello world".getBytes());
		//关闭流
		outputStream.close();
	}
}

2、System.err 表示错误输出

package com.demo.io;
public class SystemErr {
	public static void main(String args[]){
		try {
			Integer.parseInt("hello");
		} catch (NumberFormatException e) {
			System.out.println("out输出结果是:"+e);
			System.err.println("err输出结果是:"+e);
		}
	}
}

*System.out 一般的信息是愿意展示给用户看见的

*System.err 一般的信息是不愿意展示给用户看见的

3、System.in 

System.in 实际上表示的是一个键盘的输入,使用此操作,可以完成键盘输入数据功能。

package com.demo.io;

import java.io.IOException;
import java.io.InputStream;

public class SystemINDemo {
	public static void main(String args[]) throws IOException{
		InputStream inputStream = System.in;//准备键盘输入数据
		byte[] b = new byte[5];
		System.out.println("请输入内容:");
		int len = inputStream.read(b);
		System.out.println("输入的内容是:"+new String(b, 0, len));
		inputStream.close();
	}
}

此时已经实现了键盘输入的功能,但是此过程中在使用时会存在长度限制而且输入中文的时候也会存在问题。此时就可以通过另一种方式,不指定大小,边读边判断是否结束。

package com.demo.io;

import java.io.IOException;
import java.io.InputStream;

public class SystemINDemo2 {
	public static void main(String args[]) throws IOException{
		InputStream inputStream = System.in;//准备键盘输入数据
		System.out.println("请输入内容:");
		int temp = 0; //接收内容
		StringBuffer buf = new StringBuffer();
		while((temp=inputStream.read())!=-1){
			//转型
			char c = (char)temp;
			//判断是否是回车
			if(c=='\n'){
				//退出循环
				break;
			}
			buf.append(c);
		}
		System.out.println("输入的内容是:"+buf);
		inputStream.close();
	}
}

此时,数据读取的时候没有长度限制了。(但是中文乱码了)
但是在输入中文的时候就无法正确取读了,因为每次读的是一个字节,应该按照整体取读,那么如果想要完成更好的读取操作,则只能使用后续的BufferReader类完成。

输出、输入重定向

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(PrintStream in)


范例:验证输出重定向

package com.demo.io;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 * 重定向的例子
 */
public class RedirectSystemOutDemo {
	public static void main(String args[]) throws FileNotFoundException{
		File file = new File("g:"+File.separator+"demo.txt");
		System.setOut(new PrintStream(new FileOutputStream(file)));
		System.out.println("hello world");
	}
}


提示:

在开发中不建议System.err的输出位置,只建议修改System.out的输出位置

一般System.in的操作最好不要去修改



抱歉!评论已关闭.