Java I / O redirection
Translation: Cherami
Email: 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 use I / O redirections like this:
$ comMMand> Outfile
This usage is to: run a command and direct its standard output (such as the output of System.out.Println) to the specified file instead of the 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 sample redirection standard outputs to a file:
Import java.io. *;
Public class redirectdemo1 {
Public static void main (string args []) throws oException {
// Create a printStream on a file
FileOutputStream Fos =
New FileOutputStream ("out.txt");
BufferedoutputStream Bos =
New BufferedOutputStream (FOS, 1024);
PrintStream PS =
New PrintStream (BOS, FALSE);
/ / Redate system.out to this file
System.setOut (PS);
// output
System.out.Println ("this is a test / u4321");
INT n = 37;
System.out.println ("The Square of" N
"IS" (n * n));
ps.close ();
}
}
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 code implemented in JDK 1.3 is in this 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 there is such a line in your applet:
System.out.println ("this is a test"); and you run the program in the United States:
$ Java Prog> Outfile
The program is stored in the file "outfile" in the ASCII character. 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 is an example including the above two questions:
Import java.io. *;
Public class redirectdemo2 {
Public static void main (string args []) throws oException {
// 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" N "IS" (n * n));
// Get the string written
String str = sw.toString ();
// Explicit it
System.out.print (STR);
// Out of the string to the file, use UTF-8 encoding
FileOutputStream Fos =
New FileOutputStream ("out.txt");
OutputStreamWriter OSW =
New OutputStreamWriter (FOS, "UTF-8");
BUFFEREDWRITER BW =
New BufferedWriter (OSIW);
BW.WRITE (STR);
BW.CLOSE ();
// Read back and check
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 Linsep = System.getProperty ("line.separator");
IF (! STR.EQUALS (S1 LINESEP S2 LINESEP))
System.err.Println ("Equals Check Failed");
}
}
The first part of the example creates a PrintWriter object on a StringWriter, which is similar to PrintStream, but the operation is character instead of 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. Finally, the string reads back from the file or use UTF-8 encoding. Then compare with the original string. There are two line separators in the original string, so the two strings are read back, and the line separator is added to compare.
Note You can also redirect your input from a file or string, use a class like StringReader.