Java I/O stream learning Notes (ii)

Source: Internet
Author: User

Tag: Writer calculates art java break null buffer stream exception byte order

1. There are two types of streams in Java, the byte stream and the character stream, respectively. The JDK provides a byte stream: Inputstream,outputstream, reader, and writer for a character stream.
A. The way a file is stored on a hard disk is stored in a collection of byte, and the characters that are not files are kept on disk instead of characters encoded into bytes, and then the bytes are stored to disk. When reading a file (especially a text file), it is also a byte-by-byte read.
B. Buffered streams, print streams, object streams, file streams, conversion streams
2. What is the difference between a character stream and a byte stream?
A. The concept of flow: all the data in the program is transmitted or saved in a stream.
B. The byte stream and character stream are differentiated according to the different processing data. The byte stream is handled by a 8-bit bytes (range 0-255), and the character stream handles 2-byte Unicode characters (range 0-65525).
C. Byte streams can be used for any type of object, including binary objects, but it cannot handle Unicode characters directly, and character streams can only handle characters or strings.
D. The byte stream is used to process non-text files, such as Mp3,jpg,gif,mp4, and character streams are used to process text files.
3. Serialization: Putconverting Java objects to byte sequencesProcess is called serialization of an object, and the process of restoring a sequence of bytes to a Java object is called deserialization.
To implement serialization:only serializable is realized.The object of the class with the Externalizable interface can be serialized (the latter is a subclass of the former), and the class that implements the two interfaces is entirely self-contained to control the behavior of the serialization, and only the class that implements the former can take the default serialization method. The implementation of these two interfaces signifies that the object can be serialized.
Serialization uses a name calledversion number of the SerialversionuidWith each serializable class that you want to associate, the sequence number is used during deserialization to verify that the sender and the recipient of the serialized object have loaded the serialization-compatible class for the object. IfThe Serialversionuid of the class of the object loaded by the recipient differs from the version number of the corresponding sender's class, then deserialization causes invalidclassexception. A serializable class can be declared as a "serialversionuid" field (the field must bestatic (Static) and final (final) long fieldsDisplays the Serialversionuid that declares itself (it is recommended to use the method of displaying claims, not the default values that the JVM calculates).
4. Conversion flow
A. The conversion stream implements the common denominator between the byte stream and the character stream. It isa bridge between a byte stream and a character stream, the byte stream can be converted to character streams. For a better conversion operation, a buffer stream can be set for the transition flow.
B. InputStreamReader (InputStream in,string charsetname)
5. Use GetBytes (chartsetname) to encode the string as specified and in byte notation.
String st = new string (byte[] bytes,string charsetname) constructs a new string by decoding the specified byte array using the specified charset.
6. Any type of print flow can be printed: Printstream,printwriter
7. Flush usage of BufferedWriter:refreshes the buffer of the stream。 If the streamsaved buffersAll characters of the various write () methods in thewrite them to the expected target immediately。 Then, if the target is another character or byte stream, it is refreshed. Therefore, a flush () call refreshes all buffers in the writer and outputstream chains. If the intended target of this stream is an abstraction (such as a file) provided by the underlying operating system, flushing the stream can only guarantee that the bytes that were previously written to the stream are passed to the operating system for writing, but that the bytes can actually be written to a physical device, such as a disk drive.

8. Using byte stream to realize the copy function of picture

1  Public Static voidCopy (String s1,string s2)throwsexception{2File F1 =NewFile (S1);3InputStream in =NewFileInputStream (F1);4OutputStream OU =Newfileoutputstream (S2);5         byte[] buffer =New byte[1024];6         intLen = 0;7          while( -1!= (len = In.read (buffer,0, Buffer.length))) {8Ou.write (buffer,0, Len);9         }TenOu.close ();//equivalent stack, advanced back out One in.close (); A     } - //Test Code -  Public Static voidMain (string[] args)throwsexception{ theString S1 = "C:" +separator+ "Users" +separator+ "ZTF" -+separator+ "Desktop" +separator+ "Java" -+separator+ "+separator+" "Ff.txt"; -String s2 = "C:" +separator+ "Users" +separator+ "ZTF" ++separator+ "Desktop" +separator+ "Java" -+separator+ "+separator+" "Copy.txt"; + copy (S1,S2); ACopy ("C:\\users\\public\\pictures\\sample pictures\\0.jpg", at"C:\\users\\public\\pictures\\sample pictures\\copy0.jpg"); -}

9. Print multiple exception messages using the print stream

1  Public Static voidPrinttest ()throwsioexception{2File FL =NewFile ("C:\\\\desktop\\java\\54\\54.properties");3FileOutputStream fos =NewFileOutputStream (fl,true);4PrintStream PS =NewPrintStream (FOS);5         Char[] ch = {' A ', ' B ', ' C ', ' d '};6         Try{7System.out.println (1/0);8System.out.println (ch[5]);9}Catch(Exception e) {Ten e.printstacktrace (); One E.printstacktrace (PS); A}finally { - ps.close (); -         } the}

10. Get the character encoding of the data

1  Public StaticString CharsetName (String str)throwsunsupportedencodingexception{2String[] charsets = {"GBK", "gb2312", "UTF-8"};3String name =NULL;4          for(inti = 0; i < charsets.length; i++) {5             if(Str.equals (NewString (Str.getbytes (Charsets[i]), Charsets[i])) {6Name =Charsets[i];7                  Break;8             }9         }Ten         returnname; One}

This problem feeling has the question, will improve!

Java I/O stream learning Notes (ii)

Related Article

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.