Print streams
1 Overview
Usually printed output console, calling print method and println method completes, the two methods are derived from java.io.PrintStream class, can easily print the value of various data types, is a convenient output the way.
2, PrintStream class
java.io.PrintStream: print streams.
PrintStream adds functionality to another output stream, namely the ability to print in the form of various data values.
PrintStream features:
① responsible only for output data, is not responsible for reading data
② different from other output stream, PrintStream never throws IOException
③ a specific method, print, println; void print (any type of value); void println (value of any type and line feed)
3, construction method
PrintStream (File file): the output destination is a file
PrintStream (OutputStream out): the output destination is a byte output stream
PrintStream (String fileName): the output destination is a file path
4, inherit the parent class members
PrintStream extends OutputStream
Inherit the parent class member method:
public void close (): Close this output stream and releases any system resources associated with the stream.
public void flush (): Flushes this output stream and forces any buffered output bytes to be written.
public void write (byte [] b ): The b.length byte to this output stream from the specified byte array.
public void write (byte [] b , int off, int len): write len bytes from the specified byte array, an output start offset off to this output stream.
public abstract void write (int b) : the specified output stream of bytes.
Note :
① If you use the write method inherited from the parent class to write data, then view the data when queries coding table 97-> a
② If you use your own unique way print / println method of writing data, write data is output as 97-> 97
5、Demo
. 1 public static void main (String [] args) throws a FileNotFoundException {
2 System.out.println ( "the HelloWorld"); // console printout
. 3
. 4 // Create PrintStream print stream objects, the construction method to be outputted binding destination
5 PrintStream PS = new new PrintStream ( "E: \\ print.txt" );
6 // If write data using the write method inherited from the parent class, then view the data when queries coding table 97-> a
7 ps.write (97 );
. 8 // If own unique method of print / println method of writing data, the write data is output as 97-> 97
. 9 ps.println (97 );
10 ps.println (8.8 );
. 11 ps.println ( 'A' );
12 is ps.println ( "the HelloWorld" );
13 is ps.println ( to true );
14
15 // release resources
16 ps.close ();
. 17 }
6, the output destination changes
1 /*
2 可以改变输出语句的目的地(打印流的流向)
3 输出语句,默认在控制台输出
4 使用System.setOut方法改变输出语句的目的地改为参数中传递的打印流的目的地
5 static void setOut(PrintStream out)
6 重新分配“标准”输出流。
7 */
8 public class DemoPrintStream {
9 public static void main(String[] args) throws FileNotFoundException {
10 System.out.println("我是在控制台输出");
11
12 PrintStream ps = new PrintStream("E:\\目的地是打印流.txt");
13 System.setOut(ps);//把输出语句的目的地改变为打印流的目的地
14 System.out.println("我在打印流的目的地中输出");
15
16 ps.close();
17 }
18 }