Visual editor, alt+1 to 9 keys, switch to Tool area, TAB key, select button, ESC, return to edit
public class Testio {
public static void Main (string[] args) {
String filename = "D:" + file.separator + "Hello.txt";
File File = new file (filename);
SYSTEM.OUT.PRINTLN ("Standard output-console");
try {
FileOutputStream fileout = new FileOutputStream (filename);
PrintStream prt = new PrintStream (file);
System.setout (PRT);
SYSTEM.OUT.PRINTLN ("standard output Redirect--file");
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
1, the print stream is divided into PrintStream and PrintWriter two
The meanings they represent can be seen from their constructors:
printstream extends Filteroutputstream:
private PrintStream (Boolean autoflush, OutputStream out, Charset Charset) {
super (out);
this.autoflush = AutoFlush;
this.charout = new OutputStreamWriter (this, CharSet);
this.textout = new BufferedWriter (charout);
}
First it is a subclass of OutputStream
next it is a parcel stream that wraps the conversion stream on its output byte stream, converts it into a character output stream, And then wrap a layer of buffered output stream
finally his flush function is turned off by default, so it needs to be turned on or flush manually.
PrintWriter extends Writer:
public PrintWriter (OutputStream out, Boolean AutoFlush) {
this (new BufferedWriter (new OutputStreamWriter)), AutoFlush);
Save print stream for error propagation
if (out instanceof Java.io.PrintStream) {
Psout = (printstream) out;
}
}
It can be seen that PrintWriter is the same for byte stream processing and PrintStream, but it can also use writer stream as a parameter.
Public PrintWriter (Writer Out,boolean autoflush) {
Super (out);
This.out = out;
This.autoflush = AutoFlush;
LineSeparator = java.security.AccessController.doPrivileged (
New Sun.security.action.GetPropertyAction ("Line.separator"));
}
2. Comparison of functions (methods) provided by the output stream
-Writer://Filewriter\chararraywriter
Write (int) one character
Write (char[]) an array of characters
Write (Char[],offset,len) part of a character array
Write (String,offset,len) a string or part of it
-OutputStream
Write (int) of one byte
Write (byte[]) an array of bytes
Write (Byte[],offset,len) part of a byte array
-DataOutputStream
Provides the following features on a outputstream basis
Writeint (int) outputs an int type of data in bytes. 0x 00 00 00 64
Writelong (long) outputs a long type of data in bytes.
-PrintStream
First it inherits from OutputStream, is a byte stream
Second, it is output in character form. This means that it automatically converts the data in bytes to characters in the specified encoding, and then outputs it. The function of converting and buffering output is set, and the above constructor is referenced
Finally, it mainly provides a number of formatted output functions
Write (int) byte output----64
Write (Byte[],offset,len)//byte output
Write (char[]) character output
Write (string) bufferedwriter character output
Print (...) character output----100
println (...) character output ' \ r \ n '
printf (format,...) character output
-PrintWriter
It's the same way as PrintStream.
With respect to line breaks, under Windows is \ r \ n, and under Linux,
Their usage is basically the same
public class Testio {
public static void Main (string[] args) {
String filename = "D:" + file.separator + "Hello.txt";
File File = new file (filename);
try {
FileOutputStream fout = new FileOutputStream (file);
PrintStream prt = new PrintStream (fout);
Prt.write (100); The contents of the file are 64
Prt.close ();
Fout.close ();
} catch (FileNotFoundException e) {
TODO auto-generated Catch block
E.printstacktrace ();
} catch (IOException e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
}
Print flow is commonly used for standard output and standard errors, and they are output on the display by default. We can change their output to the file, which is the output redirect, or the standard output redirect
System.out Standard output stream
System.err standard error stream
System.seterr ()
System.setout ()
Test Code:
This article is from the "Technical Life" blog, please be sure to keep this source http://6621180.blog.51cto.com/6611180/1922788
Java Print streams and redirects