A description of the object-oriented approach. For polymorphism, you can use subclasses to instantiate the parent class, which is very important for object-oriented, but some of the unique methods in subclasses are out of use, and this can be done using a downward transformation and forcing the parent class to transform the data.
Byte Stream and character stream
Byte stream Inputstream/outputstream, can only manipulate byte
Character Stream Reader/writer
FileOutputStream (File f)
The byte stream itself can only operate in bytes.
For this class, if the file does not exist, it is automatically created
The Write () method overwrites the file by default
You can use the constructor method FileOutputStream (File F,boolean append) so that you can specify whether you want to use Append mode
FileOutputStream (File f)
Method-Available () Gets the number of bytes that can be read
Character Stream
Typically one character is two bytes, you can manipulate the string directly
will use the buffer
Filewriter/filereader
The default is still overwritten, to append as long as the fileoutputstream is similar to the Boolean flag is good FileReader read to char/char[]
The difference between a byte stream and a character stream
The byte stream is directly associated with the file operation, does not use the buffer character stream data first in the buffer, and then writes from the buffer to the file, remember Fiush () or close ()
byte-to-character conversion stream
- InputStreamReader
Streams an input byte into a character stream, which is a subclass of reader
- OutputStreamWriter
Streams an output byte to a character stream, which is a subclass of writer
FileWriter is a subclass of OutputStreamWriter FileReader is a subclass of Outputstreamreader
Memory Operation Flow
Bytearrayinputstream (byte[])
Writes data to memory in byte
Bytearrayoutputstream ()
The actual meaning is that the data is written into memory through the input stream, and then the output stream is taken out of the memory!
private static void Tesbytearrayinputstream () {//create Bytearrayinputstream byte stream, content is arrayletters array bytearrayinputstr EAM Bais = new Bytearrayinputstream (arrayletters); Reads 5 bytes from the byte stream for (int i=0; i<len; i++) {//If the next byte can continue to be read, the next byte if (bais.available () >= 0) { Reads "The next byte of the stream" int tmp = Bais.read (); System.out.printf ("%d:0x%s\n", I, integer.tohexstring (TMP)); }}//If the "byte stream" does not support the tagging function, exit the IF (!bais.marksupported ()) {System.out.println ("Make not supported!") directly; return; }//Mark "the next read location in the byte stream." That is, Mark "0x66" because 5 bytes have been read before, so the next one to be read is the 6th byte "//(Bytearrayinputstream), and the" parameter 0 "in the mark (0) function of the class is of no practical significance. (), Mark () and reset () are matched, and reset () resets "the next read in the byte stream" to the location saved in Mark () Bais.mark (0); Skips 5 of bytes. After skipping 5 bytes, the next value read in the byte stream should be "0x6b". Bais.skip (5); Reads 5 data from a byte stream. i.e. read "0x6b, 0x6c, 0x6d, 0x6e, 0x6F" byte[] buf = new Byte[len]; Bais.read (buf, 0, LEN); Converts a buf to a string string. "0x6b, 0x6C, 0x6d, 0x6e, 0x6F "the corresponding character is" Klmno "string str1 = new string (BUF); System.out.printf ("str1=%s\n", str1); Reset the byte stream: that is, reset the next read position in the byte stream to the location marked by Mark (), which is 0x66. Bais.reset (); Reads 5 bytes into buf from "reset byte stream". That reads "0x66, 0x67, 0x68, 0x69, 0x6A" Bais.read (buf, 0, LEN); Converts a buf to a string string. "0x66, 0x67, 0x68, 0x69, 0x6A" corresponds to the character "Fghij" string str2 = new string (BUF); System.out.printf ("str2=%s\n", str2);}
}
Pipe flow
Pipeline flow is the realization of communication between two processes
Pipeoutputstream Pipeinputstream using Connect () to connect the input and output streams, connect is the Pipeoutputstream method used to connect a pipeinputstream
Print Flow PrintStream
Defines a number of print and pringtln methods that can be printed on any data type
System.out is the printstream that locates in the output device
构造方法,指定输出的位置PrintSTream(OutputStream out)
So the printout is more convenient, PrintStream is OutputStream sub-class, can be easily output, such design is called Decorative design mode
PrintWriter with similar formatted output above
Formatted output similar to the C language%d,%s,%f,%c
ps.printf("姓名%s,年龄:%d",a,b);
System.in and BufferedReader (character stream)
is a InputStream
BufferedReader buf=null; buf=new BufferedReader(new InputStreamReader(System.in)); String str=null; try { while ((str=buf.readLine())!=null){ System.out.println(str); } } catch (IOException e) { e.printStackTrace(); }
Input and output redirection
System.setOut(new PrintStream(new FileOutputStream("D:/a.txt")));
In fact, out, err is the system's three constants, can be changed by the set method, and then the role of redirection is completed.
Scanner
In the Java.util package, you can implement the full functionality of BufferedReader, which is a tool class
- The default delimiter is a space and can be set by itself
- Very powerful, can accept many types of data, but for the date type is not supported, you can use Scanner.hasnext (pattern)/next (pattern) match to take out the data
Can accept file, do a series of files operation
Scanner scanner=new Scanner(System.in);scanner.useDelimiter("\n");//修改分割符int a=0;float f=0f;String date=null;if(scanner.hasNextInt()){ a=scanner.nextInt();}if(scanner.hasNextFloat()){ f=scanner.nextFloat();}if(scanner.hasNext("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}")){ date=scanner.next("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}");}System.out.println(a+" "+f+" "+date);try { Date date1=new SimpleDateFormat("yy-MM-dd hh:mm:ss").parse(date); System.out.println(date1);} catch (ParseException e) { e.printStackTrace();}
Merging stream compression streams
Supports three formats of compression, Zip/jar/gzip, zip here for example the other two are the same
ZipEntry, is the compressed file directory of each file
- Zipoutputstream (OutputStream)
Files, folders can be compressed
ZipFile class
Class that specifically represents a compressed file
Fallback Stream character encoding
获得本机编码System.getProperity("file.encoding")
Common encoding formats
- Iso8859-1 single-byte encoding, can only be expressed in English
- GBK GB, denotes Chinese characters
- Unicode 16 binary Two byte encoding, most standard, but incompatible iso-8859-1
- UTF variable length encoding, a character 1--6, can represent all language characters,
About the structure of IO stream
Input stream
- InputStream
- Bytearrayinputstream (byte[])
- FileInputStream (File)
- PipedInputStream
- Sequenceinputstream (Inputstream,inputstream)
- FilterInputStream
- Bufferedinputstream (InputStream)
- Pushbackinputstream
- DataInputStream
Output stream
- OutputStream
- Bytearrayoutputstream
- FileOutputStream
- PipedOutputStream
- Filteroutputstream
- Bufferedoutputstream
- PrintWriter
- DataOutputStream
A common approach
InputStream is a class for inputting byte data, so the InputStream class provides 3 overloaded read methods. Common methods in InputStream class: 1 pu Blic abstract int Read (): reads a byte of data, and the return value is the int type value of the high 0. 2 public int Read (byte b[]): reads b.length bytes of data into a B array. The return value is the number of bytes read. The method is actually called the next method implementation of the 3 public int read (byte b[], int off, int len): reads up to len bytes of data from the input stream and stores it in the B array with an offset of off 。 4 public int available (): Returns the number of bytes that can be read in the input stream. Note: If the input is blocked, the current thread will be suspended, and if the InputStream object calls this method, it will only return 0, and this method must be used by the subclass object that inherits the InputStream class, 5 public long ski P (long N): ignores n bytes in the input stream, the return value is the number of bytes actually ignored, skips some bytes to read 6 public int close (): We must close the stream we have opened after we have finished using it. OutputStream provides 3 write methods to do the output of the data, which is relative to InputStream. 1. public void Write (Byte b[]): Writes the bytes in parameter B to the output stream. 2. public void Write (byte b[], int off, int len): writes Len bytes from the offset off of parameter B to the output stream. 3. Public abstract void Write (int b): convert int to byt firstType E, which writes low bytes to the output stream. 4. public void Flush (): Outputs all data in the data buffer and empties the buffer. 5. public void Close (): closes the output stream and frees the system resources associated with the stream.
The Java Stream class provides a structured approach, such as the underlying stream and the high-level filtering stream.
Instead of reading from the input device, the high-level stream reads from the other stream. The same high-level output stream is not written to the output device, but to other streams.
Using "layered objects (layered objects)", the practice of adding functionality to a single object dynamically and transparently, is known as decorator
Pattern. The decorator mode requires that all objects that are wrapped outside the original object must have exactly the same interface. This makes
The usage of decorator becomes very transparent-the message passed to it is always the same regardless of whether the object has been decorate. This is also Java I/O
The reason for the class library to have a "filter" Class: The Abstract "Filter" class is the base class for all decorator
- DataInputStream contains a complete set of interfaces for reading primitive data
- Bufferedinputstream, added a buffer for InputStream, the size of the buffer is 8k
- Pushbackinputstream has a "clamp single-byte" buffer that can be pressed back to the last byte read.
- The DataOutputStream includes a full set of interfaces written to primitive data.
- PrintStream (Inpurstream,boolean autoflush) Print stream
- Bufferedoutputstream with Buffer
Java IO Stream