天天看點

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");
  }
}
           

繼續閱讀