Java Learning Note -10.io Stream

Source: Internet
Author: User

1. The input stream, which can only read data from it, cannot be written to it. The output stream can only be remembered for writing byte data, and cannot be read from it.

The 2.InputStream types are:

     The bytearrayinputstream contains a memory buffer from which to remove the bytes. FileInputStream          Gets the bytes from the file.        The ObjectInputStream is used to restore the serialized object. PipedInputStream         the pipeline input stream and reads the contents of the pipeline. Multi-and PipedOutputStream for multi-threaded communication. The Sequenceinputstream      is a logical concatenation of multiple input streams, read from the first input stream, up to the last input stream.  the bytes read by StringBufferInputStream are provided by a string.

The 2.OutputStream types also have the following:

Bytearrayoutputstream     The class implements an output stream whose data is written to a buffer that is served by a byte array, and the buffer grows automatically as the data is written continuously. FileOutputStream          This class implements an output stream whose data is written to a file. ObjectOutputStream        This class will implement serialization of the serialized object after it is written to the specified place.  the         output stream of the PipedOutputStream pipeline is the sending end of the pipeline.

3.objectstream,inputstream are often used with FileInputStream and FileOutputStream. (used when initializing)

New ObjectOutputStream (new FileOutputStream ("logon.out"));

The 4.File class does not refer to a file, it also represents the name of a series of files, and if it represents a set of files, the list () method can be used to query the set.

5.StreamTokenizer is used to divide any inputstream into a series of sizes, StringTokenizer provides functionality similar to Streamtokenizer, but is similar to the split () method of C # for strings.

6. Steps to compress a file with zip:

First generate the compressed file:

FileOutputStream f =new fileoutputstream ("test.zip"=new   newout =new  zipoutputstream (New Bufferedoutputstream (csum));
Out.setcomment ("A test of Java zipping");//Compressed file comment information

Then add the file you want to compress:

out. Putnextentry (new zipentry (test.txt);

You can then continue to write something else to the file if you need to, but you must ensure that the ZipEntry class is already in the out class.

Source:

ImportJava.io.*;ImportJava.util.*;Importjava.util.zip.*; Public classzipcompress { Public Static voidMain (string[] args) {Try{FileOutputStream FileOutputStream=NewFileOutputStream ("Test.zip"); Checkedoutputstream Csum=NewCheckedoutputstream (FileOutputStream,NewAdler32 ()); Zipoutputstream OutputStream=NewZipoutputstream (NewBufferedoutputstream (csum)); Outputstream.setcomment ("A test of Java zipping"); System.out.println ("Writing file" + "Zip.txt"); BufferedReader in=NewBufferedReader (NewFileReader ("Zip.txt")); Outputstream.putnextentry (NewZipEntry ("Zip.txt")); //int C; //While ((c = In.read ())! =-1) {//Outputstream.write (c); // }            //in.close ();Outputstream.write ("123". GetBytes ());            Outputstream.close (); System.out.println ("Checksum:" +csum.getchecksum (). GetValue ()); System.out.println ("Reading file"); FileInputStream FileInputStream=NewFileInputStream ("Test.zip"); Checkedinputstream Csumi=NewCheckedinputstream (FileInputStream,NewAdler32 ()); Zipinputstream in2=NewZipinputstream (NewBufferedinputstream (Csumi));            ZipEntry ZipEntry; System.out.println ("Checksum:" +csumi.getchecksum (). GetValue ());  while((ZipEntry = In2.getnextentry ())! =NULL) {System.out.println ("Reading file:" +zipentry); intx;  while((x = In2.read ())! = 1) {System.out.println (x);            }} in2.close (); ZipFile ZF=NewZipFile ("Test.zip"); Enumeration e=zf.entries ();  while(E.hasmoreelements ()) {ZipEntry ze2=(ZipEntry) e.nextelement (); System.out.println ("File:" +ze2); }        } Catch(Exception e) {e.printstacktrace (); }    }}

The difference between 7.Serialization and externalizable interfaces, both are interfaces that can serialize classes, except that serialization serializes all of the objects by default. Externalizable is the ability to control the serialized parts, that is, partial serialization.

8.serialization Control Serialization Method: (1) apply transient (temporary) keyword.

(2) We can implement the Serializable interface and add (Note that "add", not "overwrite" or "implement") methods named WriteObject () and ReadObject (). Once the object is serialized or reassembled, the two methods are called separately.

Java Learning Note -10.io Stream

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.