One, memory operation Flow (Bytearrayinputstream, Bytearrayoutputstream)
(a), public class Bytearrayinputstream extends InputStream;
Bytearrayinputsteam: The class is a subclass of InputStream, which reads data from an in-memory byte array, so its data source is a byte array. The methods for constructing this class include:
Bytearrayinputstream (byte[] buf)--------parameter buf specifies the data source for the byte array type.
Bytearrayinputstream (byte[] buf, int offset, int lenght)-----parameter buf Specifies the byte array type data source, and the parameter offset specifies the starting subscript position at which to start reading data from the array, Lenght Specifies the number of bytes to read from the array.
The Bytearrayinputstream class itself uses the adapter design pattern, which converts the byte array type to the input stream type, allowing the program to read the byte array.
Public Static voidBytearrtest () throws ioexception{Bytearrayinputstream bis=NewBytearrayinputstream ("ABCD". GetBytes ()); intlen=0; while((Len=bis.read ())!=-1) {System. out. println (Char) len+"<=>"+Len); } bis.close (); }
(b), public class Bytearrayoutputstream extends outputstream.
Bytearrayoutputstream: This class implements an output stream in which the data is written to a byte array. The buffer grows automatically as data is written continuously. Data can be used toByteArray() and toString() obtained. Closing bytearrayoutputstream is not valid. The methods in this class can still be called after the stream is closed, without generating any ioexception.
1, public byte[] Tobytearray() creates a newly allocated byte array. Its size is the current size of this output stream, and the valid contents of the buffer are copied into the array
Public Static voidBytearrout ()throwsioexception{bytearrayoutputstream Bos=NewBytearrayoutputstream (); Bos.write (97); Bos.write (New byte[]{98,99,100}); //Public byte[] Tobytearray () creates a newly allocated byte array. Its size is the current size of this output stream, and the valid contents of the buffer are copied into the array. byte[] bys=Bos.tobytearray (); for(byteB:bys) {System.out.println (b+ "<==>" + (Char) b); } }
2. The public string toString() uses the default character set of the platform to convert the buffer content to a string by decoding bytes. The length of the new String is a function of the character set and therefore may not be equal to the size of the buffer.
public static void byteArrOut2 () throws ioexception{bytearrayoutputstream Bos =new Bytearrayoutputstream (); Bos.write ( 97 new byte []{98,99,100< Span style= "COLOR: #000000" >}); // public String toString () uses the default character set of the platform, Converts the buffer content to a string by decoding bytes. The length of the new String is a function of the character set and therefore may not be equal to the size of the buffer. String str=bos.tostring (); System.out.println (str); // ABCD }
(iii), Bytearrayinputstream, Bytearrayoutputstream use together
Public Static voidBytearrout ()throwsioexception{bytearrayoutputstream Bos=NewBytearrayoutputstream (); Bos.write (97); Bos.write (New byte[]{98,99,100}); byte[] bys=Os.tobytearray (); Bytearrayinputstream bis=NewBytearrayinputstream (bys); intLen=0; while((Len=bis.read ())!=-1) {System.out.print (Char) len);//ABCD } }Second, print flow (printstream,printwriter)
PrintStream is a subclass of Filteroutputstream.
PrintWriter is the subclass of writer.
Characteristics of the print stream:
* A: Only the data is written, no data is read. Only the destination can be manipulated and the data source cannot be manipulated. Print stream only byte print stream printstream with character print stream PrintWriter
* B: You can manipulate any type of data. Their print () series Methods and println () series methods can manipulate arbitrary types of data.
* C: Automatically refreshes if automatic refresh is enabled. But you must call the println () method
PrintWriter pw = new PrintWriter (New FileWriter ("Pw2.txt"), true);
PrintStream ps=new PrintStream (New FileOutputStream ("A.txt"), true);
* D: The stream is able to manipulate the text file directly.
Public printstream(file file) throws FileNotFoundException.
Public printwriter(file file) throws FileNotFoundException.
1, using PrintStream to implement the copy of the file
Public Static voidStreamcopy ()throwsioexception{Bufferedinputstream bis=NewBufferedinputstream (NewFileInputStream ("A.txt")); //Public PrintStream (outputstream out,boolean autoflush) Use this function to turn on auto-refresh functionPrintStream ps=NewPrintStream (NewFileOutputStream ("Copy.java"),true); byte[] bys=New byte[1024]; intLine =0; while((Line=bis.read (bys))!=-1) {ps.println (NewString (bys,0, line)); } ps.close (); Bis.close (); }
2, using PrintWriter to implement the copy of the file
Public Static voidCopy ()throwsioexception{bufferedreader BR=NewBufferedReader (NewFileReader ("A.txt")); //Package Destination//Public printwriter (OutputStream out,boolean autoflush); Use this function to turn on auto-refresh functionPrintWriter PW =NewPrintWriter (NewFileWriter ("Copy.java"),true); String Line=NULL; while((Line=br.readline ())! =NULL) {pw.println (line); } pw.close (); Br.close (); }iii. standard input and output stream (Public final class System)
Three member variables in the system class
//the "standard" input stream. This stream is open and ready to provide input data. Typically, this stream corresponds to keyboard input or another input source specified by the host environment or user. Public Final StaticInputStream in =NULL; //"standard" output stream. This stream is open and ready to accept output data. Typically, this stream corresponds to the display output or another output target specified by the host environment or user. Public Final StaticPrintStream out =NULL; //the "standard" error output stream. This stream is open and ready to accept output data. Typically, this stream corresponds to the display output or another output target specified by the host environment or user Public Final StaticPrintStream err =NULL;
By the System.out variable we can:
// Here's what we know, this output statement is essentially an IO stream operation that outputs data to the console. System.out.println ("HelloWorld"); // get standard output stream object PrintStream PS = System.out; Ps.println ("HelloWorld");
Other streams of Java IO streams