Java learning notes-10. io stream, java learning notes-10.io

Source: Internet
Author: User

Java learning notes-10. io stream, java learning notes-10.io

1. The input stream can only read data from it, but cannot write data to it. The output stream can only read bytes of data.

2. The InputStream types include:

ByteArrayInputStream contains a memory buffer, which is extracted from byte. FileInputStream obtains bytes from the file. ObjectInputStream is used to restore serialized objects. PipedInputStream: the input stream of the pipeline to read the content of the pipeline. More than PipedOutputStream is used for multi-thread communication. SequenceInputStream is a logical concatenation of Multiple Input streams. It is read from the first input stream until the last input stream. The Bytes read by StringBufferInputStream are provided by the string.

2. The OutputStream type is also as follows:

The ByteArrayOutputStream class implements an output stream, and its data is written into the buffer zone acting as a byte array. The buffer zone will automatically grow as data continues to be written. The FileOutputStream class implements an output stream whose data is written to a file. The ObjectOutputStream class writes serialized objects to a specified place after serialization. The output stream of the PipedOutputStream pipeline is the sending end of the pipeline.

3. Objectstream and InputStream are often used together with FileInputStream and FileOutputStream. (Used during initialization)

ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("Logon.out"));

4. The File class is not a File, but also a series of File names. If it represents a File set, you can use the list () method to query this set.

5. StreamTokenizer is used to divide any InputStream into a series of numbers. StringTokenizer provides a very similar function to StreamTokenizer, But it targets strings and feels similar to the split () method of C.

6. Zip compression steps:

First, generate the compressed file:

FileOutputStream f =new FileOutputStream("test.zip");CheckedOutputStream csum =new CheckedOutputStream(f, new Adler32());ZipOutputStream out =new ZipOutputStream(new BufferedOutputStream(csum));
Out. setComment ("A test of Java Zipping"); // compress file comments

Then add the file to be compressed:

out.putNextEntry(new ZipEntry(test.txt);

If necessary, you can continue to write other things to the file, but you must ensure that the ZipEntry class already exists in the out class.

Source code:

import java.io.*;import java.util.*;import java.util.zip.*;public class ZipCompress {    public static void main(String[] args) {        try {            FileOutputStream fileOutputStream = new FileOutputStream("test.zip");            CheckedOutputStream csum = new CheckedOutputStream(fileOutputStream, new Adler32());            ZipOutputStream outputStream = new ZipOutputStream(new BufferedOutputStream(csum));            outputStream.setComment("A test of java Zipping");            System.out.println("Writing file" + "zip.txt");            BufferedReader in = new BufferedReader(new FileReader("zip.txt"));            outputStream.putNextEntry(new ZipEntry("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 = new FileInputStream("test.zip");            CheckedInputStream csumi = new CheckedInputStream(fileInputStream, new Adler32());            ZipInputStream in2 = new ZipInputStream(new BufferedInputStream(csumi));            ZipEntry zipEntry;            System.out.println("Checksum:" + csumi.getChecksum().getValue());            while ((zipEntry = in2.getNextEntry()) != null) {                System.out.println("Reading file:" + zipEntry);                int x;                while ((x = in2.read()) != -1) {                    System.out.println(x);                }            }            in2.close();            ZipFile zf = new ZipFile("test.zip");            Enumeration e = zf.entries();            while (e.hasMoreElements()) {                ZipEntry ze2 = (ZipEntry) e.nextElement();                System.out.println("File: " + ze2);            }        } catch (Exception e) {            e.printStackTrace();        }    }}

7. the difference between the Serialization and Externalizable interfaces is that both interfaces can serialize classes. The difference is that Serialization will serialize all objects by default, externalizable can control the serialization part, that is, partial serialization.

8. serialization: (1) apply the transient (temporary) keyword.

(2) We can implement the Serializable interface and add (note: "add" instead of "Overwrite" or "IMPLEMENT") methods named writeObject () and readObject. Once the object is serialized or re-assembled, the two methods will be called separately.

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.