Translation: Cheramiemail: Cherami@163.net Original: http://developer.java.sun.com/developer/techtips/2000/tt0815.html If you often use UNIX or Windows Shells, you may often This uses I / O redirects: $ command> Outfile This is to say: Run a command and direct its standard output (such as system.out.println output) to the specified file instead of output to the console or screen . This feature is very useful. This purpose can also be reached in the Java program without relying on with the shell. Usually if the programming style you use depends on standard input output (just like UNIX Shell and other toolkit), you may not need to redirect in the program. But sometimes you want this, let us see a few examples. The first example to redirect a standard output file: import java.io. *; public class RedirectDemo1 {public static void main (String args []) throws IOException {// create a PrintStream FileOutputStream fos = new FileOutputStream on the file ( "out.txt"); bufferedoutputstream bos = New BufferedoutputStream (FOS, 1024); PrintStream PS = New PrintStream (BOS, FALSE); // Redirect System.out to this file system.setOut (PS); // Output System System .out.println ("this is a test / u4321"); int N = 37; System.out.Println ("THE SQUARE OF" N "IS" (n * n)); ps.close () }} The standard output is a reference to the PrintStream object. In order to redirect the output, create such an object to represent a file or other output stream (eg, a network connection). Then use the System.Setout call to change the System.out reference. java.lang.System codes implemented in JDK 1.3 is of the form: FileOutputStream fdOut = new FileOutputStream (FileDescriptor.out); setOut0 (new PrintStream (new BufferedOutputStream (fdOut, 128), true)); This code initializes System.out . Therefore, in the default, system.out is a printStream, which is a fileOutputStream file created from FileDescriptor.out, with 128-byte buffers, automatic refresh (when new line characters or byte vectors Automatic output).
When the output is redirected, System.out becomes a reference to a newly created PrintStream object that is passed using System.setOutout. REDIRECTDEMO1 has several questions, first of all, PrintStream uses platform default character encodes to convert characters to bytes. This is usually a good thing, for example, if you have such a line in your applet: System.out.Println ("this is a test"); and you run the program in the United States: $ java program> Outfile program in the file "outfile" Save the ASCII characters. This may be the 7-bit ASCII text file you want. But if there is a Unicode character '/ u4321' in the program, it will be converted to a '?'. If you can see a file you can see? In other words, the default encoding does not have the correct processing of that output character. Another problem is that I / O redirements can be promoted, for example, you can direct the output to a string instead of a file. Here, there is an example includes two problems above: Import java.io. *; public class redirectdemo2 {public static void main (string args []) throws ioException {// Create a printwriter on StringWriter.
StringWriter SW = new stringWriter (); PrintWriter PW = new PrintWriter (SW); // Output something to StringWriter Pw.println ("this is a test / u4321"); int N = 37; PW.Println ("The Square" Of " " IS " (n * n))); // Get the written string String string str = sw.toString (); // Explicitly system.out.print (STR); // string output to a file, FileOutputStream fos = new FileOutputStream ( "out.txt") using UTF-8 encoding; OutputStreamWriter osw = new OutputStreamWriter (fos, "UTF-8"); BufferedWriter bw = new BufferedWriter (osw); bw .write (str); bw.close (); // reads back string and checks fileinputstream fis = new fileinputstream ("out.txt"); inputStreamReader ISR = New InputStreamReader (Fis, "UTF-8"); bufferedReader Br = New BufferedReader (ISR); String S1 = Br.Readline (); String S2 = br.readline (); br.close (); string linesep = system.getProperty ("line.separetor"); if (! Str.equals (S1 LINESEP S2 LINESEP) System.err.println ("Equals Check Failed");}} The first part of the example is established a PrintWriter object on a StringWriter, which is similar to PrintStream, but the operation is character rather than byte stream. StringWriter is used to aggregate characters to a dynamic internal buffer, and then recover to a string or StringBuffer. After the output is written to StringWrit, the aggregated string is restored, then the string uses OutputStreamWriter and UTF-8 encoding to be written. This encoding is supported in all Java implementations. It encodes the characters in the '/ u0001' to '/ u007f' range into one byte and other characters are two or three bytes.