您的位置:首页 > 编程语言 > Java开发

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

2011-12-11 20:46 274 查看

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的子类)

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

public class Test26 {
public static void main(String[] args) throws IOException {
OutputStream out=System.out;
out.write("Hello World!".getBytes());
out.close();
}
}


这其实就是多态的一种表现

System.err

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

public class Test27 {
public static void main(String[] args) {
String str="Hello World";
try{
int a=Integer.parseInt(str);
}catch(Exception e){
System.err.println(e);
}
}
}


但是有些人会问这跟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是从文件中读取的

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

public class Test28 {
public static void main(String[] args) throws IOException {
InputStream in=System.in;
byte[] b=new byte[1024];
int len=in.read(b);
System.out.println(new String(b,0,len));
}
}


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

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

public class Test29 {
public static void main(String[] args) throws IOException {
InputStream in=System.in;
StringBuilder buf=new StringBuilder();
int temp=0;
while((temp=in.read())!=-1){
char c=(char) temp;
if(c=='\n')break;
buf.append(c);
}
in.close();
System.out.println(buf.toString());
}
}


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

输入输出重定向

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

System.out重定向

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

public class Test30 {
public static void main(String[] args) throws FileNotFoundException {
File f = new File("d:" + File.separator+"test.txt");
System.setOut(new PrintStream(f));
String str="This is a test!";
System.out.println(str);
}
}


[b]System.err重定向
[/b]

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class Test31 {
public static void main(String[] args) {
ByteArrayOutputStream out=new ByteArrayOutputStream();
System.setErr(new PrintStream(out));
System.err.println("Test---------------");
System.out.println(out.toString());
}
}


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

[b][b]System.in重定向[/b]
[/b]

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

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


一般,使用最多的还是System.out的重定向~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: