Io Stream Summary Supplement

Source: Internet
Author: User

1. The file Class (Java.io.File) can represent files or directories (in Java the files and directories belong to this class, and the distinction is not very obvious). The method under file is to perform disk operations on the file on disk, but the contents of the file cannot be read. Note: Creating a File object and creating a file in Java is two different concepts. The former creates a file in the virtual machine, but does not actually create it to the OS's file system, and as the virtual machine shuts down, the created object disappears. Creating a file is really a file created in the system.     For example: File F=new files ("11.txt");//Create an object named 11.txt f.createnewfile (); The file is really created in 2. Method of File Boolean createnewfile ()//Create File Boolean mkdir ()//Create directory Boolean mkdirs ()//Create multiple directories Boolean delete ()/delete file Boolean Deleteonexit (); Deleting files when the process exits, such operations are usually used in the deletion of temporary files. String[] List (): Returns the file and directory name (relative path) that are present under the current file object file[] Listfiles (): Returns all the files objects of the current file object and can be used to access the file name using GetName (). Isdirectory () and Isfile () to determine whether it is a directory or a file. String getParent () gets the parent class filename file Getparentfile () ... String GetPath () ... Path exists () determines whether a file exists 15.2. Handling cross-Platform for command: file F2=new file ("d:\\abc\\789\\1.txt") This command is not cross-platform because different OS separators are not the same. Returns the current platform file delimiter using the Separtor property of the file class.       File newd = new file ("AA" +file.separator+ "BB" +file.separator+ "cc");       File Newf = new file (newd, "mudi.txt");       try{Newd.mkdirs ();       Newf.createnewfile (); }catch (Exception e) {}3. The object's serialized interface serializable interface has no method and is an identity interface. Serialization steps: 1) implement Serializable interface 2) Instantiate object file output object 3) Output object to file 4) Some temporary variable lifecycles do not need to exceed the life cycle of the virtual machine and need to be added: The transient keyword, which is not serialized. An object that serializes an internal property of an object also needs to serialize the interface.              4.I/O Stream Base input/output: Refers to the data exchange between the boundaries of the JVM and the outside world. Output input Note: The input/output is for the JVM. 15.5. Stream classification 1) from data type: byte stream and character stream byte stream class: Abstract parent Class: Inputstream,outputstream implementation class: Bufferedinputstream Buffered stream-Bufferedoutputstreambytearrayinputstream byte array Stream-node flow Bytearrayoutputstreamdatainputstream Processing Java standard data stream-dataoutputstreamfileinputstream processing file IO Stream-node stream fileoutputstreamfilterinputstream Implementation of the overflow-byte-over-stream parent class Filteroutputstreampipedinputstream pipeline flow Pipedoutputstreamprintstream contains print () and println () Randomaccessfile supports random file abstraction parent class: Reader, Writer Implementation class: Bufferedreaderbufferedwriterprintwriterchararrayreaderchararraywriterfilereaderfilewriterfilterreaderfilterwrit  Erinputstreamreaderoutputstreamwriterpipedreaderpipedwriterstringreaderstringwriter 2) from the data direction: input stream and output stream InputXXXXX, OUTPUTXXXXX3) from the function of the stream: node stream and filter stream (use to painter mode) node stream is used to transfer data. The filter stream is used to encapsulate a node stream or other filtering stream, which adds a function to the node stream or other filtering flow. InputstThe Ream class is the parent class for all byte input streams, such as: FILEINPUTSTREAM,OBJECTINPUTSTREAM,PIPEDINPUTSTREAN1) three basic read () method A. int read (): A byte read from the stream or-1; (actual read how long) b. int read (byte[]): reads the data into a byte array and returns the number of bytes read (expected read how long) c. int read (byte[], int, int): Two int parameter specifies the sub- Range.       2) Other method A. void Close (): Closes the stream, such as using a filter flow, closes the stream at the top of the stack, and closes the rest of the stream.       B. int available (): Returns the number of bytes that can be read from the stream.       C. Skip (Long): Discards the specified number of characters in the stream. D. Boolean marksupported () e. void mark (int) f. void Rese () 2. OutputStream method Answer: 1) three basic read () method A. void Write (): B. Void write (byte[]): C. Void Write (byte[], int, in    T): writes the output stream.       2) Other method A. void Close (): Closes the stream, such as using a filter flow, closes the stream at the top of the stack, and closes the rest of the stream. b. void flush (): Allows you to force a write operation. Note: The close () method in the stream is controlled by the programmer. Because the input and output stream has gone beyond the boundaries of the JVM, it may not be possible to reclaim resources at times. Principle: Any resource that crosses the boundaries of a virtual machine requires the programmer to shut itself down and not expect garbage collection.    3. FileInputStream and FileOutputStream A: 1) node stream, using disk files.    2) to construct a fileinputstream, the associated file must be present and readable. 3) to construct a fileoutputstream and the output file already exists, it will be overwritten.    FileInputStream infile = new FileInputStream ("Myfile.dat"); FileoutpUtstream outfile = new FileOutputStream ("Results.dat");  FileOutputStream outfile = new FileOutputStream ("Results.dat", true); True when output is added, false when overridden. FileOutputStream Class Code: (Why can I build a file) public FileOutputStream (String name) {This (name!=null? New File (String): Null,false);} Keyboard Flow PrintWriter:System.in4. DataInputStream and DataOutputStream are filtered streams. To read and write Java basic classes through streams, note that the DataInputStream and DataOutputStream methods are paired. Filter the stream. Output input various data types. Writeboolean (Boolean B)------with 1bit data transfer writebyte (int)------with 1 byte data transfer writebytes (String s)-------- The data is transmitted in byte sequence Writechar (int v) ―――――― with 2 byte writechars (String s)-------------with 2 byte sequence writedouble (double D)-------with 8 by Tewriteint (int v) writelong (long L) Writeshort (short s) writeutf (String)-----------can output Chinese! 6. ObjectInputStream and ObjectOutputStream filter streams. Objects persisted Object o = new Object (); FileOutputStream fos=new FileOutputStream ("Object.txt"); ObjectOutputStream oos=new ObjectOutputStream (FOS); O Os.writeobject (o); Oos.close (); FileInputStream fis =new fileinputstream ("Object.txt"); ObjectInputStream ois =new ObjectInputStream (FIS); object o = (object) ois.readobject (); Ois.close (); 7. Bufferinputstream and Bufferoutputstream filter streams can improve the efficiency of I/O operations to add a buffering function to the node stream. A buffer is created inside the VM, and the data is written to the buffer, which is efficient when the buffer data is full and then written out once. Using an input-output stream with buffers can be significantly faster, and the larger the buffer, the higher the efficiency. (This is a typical time to sacrifice space) remember: Using a stream with a buffer, if the data data is entered, use the Flush method to write the contents of the buffer once to the external data source. The same effect can be achieved with close () because flush is used every time close. Be sure to close the external filter flow. 8. PipedInputStream and PipedOutputStream are used to communicate between threads. PipedOutputStream pos=new PipedOutputStream (); PipedInputStream pis=new PipedInputStream (); Try{pos.connect (PiS); new Producer (POS). Start (); new Consumer (PiS). Start ();} catch (Exception e) {e.printstacktrace ();} 9.RandomAccessFile random Access can get the file pointer. Long Getfilepointer () Gets the position from the beginning of the file to the file pointer. Seek (Long Point) moves the file pointer here.    Reader and writer 1) Java technology uses Unicode to represent strings and characters, and provides a 16-bit version of the stream to handle characters in a similar way.    2) InputStreamReader and OutputStreamWriter as the interface between the byte stream and the character flow. 3) If you construct a reader and writer connected to a stream, the conversion rule switches between byte encoding and Unicode defined using the default platform. 4) The difference between a byte stream and a character stream: encoding is converting characters into digital storage to a computer. The process of converting numbers to corresponding characters is called decoding. Encoding type: ASCII (digital, English): 1 characters in one byte (all encoding sets are ASCII compatible) iso8859-1 (European): 1 characters in one byte gb-2312/gbk:1 characters accounted for two bytes unicode:1 characters accounted for two bytes (slow network transmission) UTF-8: variable length byte, for English one byte, for Chinese characters two or three bytes. 10 are filtered streams. Method of BufferedReader: ReadLine (): String PrintWriter method: println (.... String,object etc.) and write () 11.    Random Access file 1) implemented two interfaces: Datainput and DataOutput;    2) As long as the file can be opened to read and write;    3) Through the file pointer can read and write files to specify the location;    4) access to all read () and write () operations in DataInputStream and DataOutputStream;       5) In the file Move method: A. Long Getfilepointer (): Returns the current position of the file pointer.       B. void seek (Long POS): Sets the file pointer to the given absolute position. C. Long Length (): Returns the length of the file. 12. Encoding problem: Encoding: Each character corresponds to an integer. Different countries have different codes, when the encoding and decoding methods are not uniform, garbled. Since the United States first developed software, so each encoding is up-to-compatible ASCII so the English is not garbled. Iso-8859-1 Western character GB2312 ... Rong GBK big5unicodeutf-8 Supplement: Byte stream end return-1 character stream end return null object stream end return eofexception extension---------) Exceptions are often used in process control, and exceptions are also a form of return for methods.

Io Stream Summary Supplement

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.