天天看点

Java I/O系统----------- 标准I/O

        程序的所有输入都可以来自于标准输入,它的所有输出都可以发送到标准输出,以及所有的错误信息都可以发送到标准错误。标准I/O的意义在于:可以很容易的把程序串联起来,一个程序的标准输出可以成为另一个程序的标准输入。

1、从标准输入中读取

        按照标准I/O模型,Java提供了System.in,System.out和System.err。其中System.out,System.err已经被包装成PrintStream对象,而System.in是一个InputStream。

        通常会用readLine()一次一行地读取输入,将System.in包装成BufferedReader因此必须用InputStreamReader把System.in转换成Reader。

import java.io.*;


public class Echo {
	public static void main(String[] args) throws IOException {
		BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
		String s;
		while ((s = stdin.readLine())!= null && s.length() != 0) {
			System.out.println(s);
		}
	}
}
           

2、将System.out转换成PrintWriter

        System.out是一个PrintStream,而PrintStream是一个OutputStream。PrintWriter有一个可以接受OutputStream作为参数的构造器,因此可以把System.out转换成PrintWriter。

import java.io.*;

public class Echo {
	public static void main(String[] args) throws IOException {
		PrintWriter out = new PrintWriter(System.out,true);
		out.println("呵呵");
	}
}
           

        重要的是要使用有两个参数的PrintWriter构造器,并将第二个参数设为true,以便开启自动清空功能,否则,可能看不到输出。

3、标准I/O重定向

        Java的System类提供了一些简单的静态方法,允许对标准输入、输出和错误I/O流进行重定向:setIn(InputStream)、setOut(InputStream)、setErr(InputStream):

import java.io.*;

public class Redirecting {

	public static void main(String[] args) throws IOException {
		PrintStream console = System.out;
		BufferedInputStream in = new BufferedInputStream(new FileInputStream("Redirecting.java"));
		PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("test.out")));
		System.setIn(in);
		System.setOut(out);
		System.setErr(out);
		
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String s;
		while ((s = br.readLine()) != null) {
			System.out.println(s);
		}
		out.close();
		System.setOut(console);
	}

}
           

注意I/O重定向操纵的是字节流而不是字符流。

4、进程控制

public class OSExecuteException extends RuntimeException {
  public OSExecuteException(String why) { super(why); }
}
           
import java.io.*;

public class OSExecute {
  public static void command(String command) {
    boolean err = false;
    try {
      Process process =
        new ProcessBuilder(command.split(" ")).start();
      BufferedReader results = new BufferedReader(
        new InputStreamReader(process.getInputStream()));
      String s;
      while((s = results.readLine())!= null)
        System.out.println(s);
      BufferedReader errors = new BufferedReader(
        new InputStreamReader(process.getErrorStream()));
      // Report errors and return nonzero value
      // to calling process if there are problems:
      while((s = errors.readLine())!= null) {
        System.err.println(s);
        err = true;
      }
    } catch(Exception e) {
      // Compensate for Windows 2000, which throws an
      // exception for the default command line:
      if(!command.startsWith("CMD /C"))
        command("CMD /C " + command);
      else
        throw new RuntimeException(e);
    }
    if(err)
      throw new OSExecuteException("Errors executing " +
        command);
  }
}
           
public class OSExecuteDemo {
  public static void main(String[] args) {
    OSExecute.command("javap OSExecuteDemo");
  }
}
           

继续阅读