Java learning summary, java Summary

Source: Internet
Author: User

Java learning summary, java Summary

The Java class library has four abstract parent classes that indicate streams: InputStream, OutputStream, Reader, and Writer.

InputStream and OutputStream areBytesInput stream and output stream for operations; Reader and Writer areCharacterThe input and output streams of the operation.

They are abstract classes and must be instantiated when they are used. Therefore, the Class Library also provides their subclasses with different functions, for example, input and Output FileInputStream, FileOutputStream, FileReader, and FileWriter as files.

--------------------------------------------------------------

  1. byte stream

  1. InputStream and OutputStream can only transmit data in bytes.

InputStreamAbstractionThe way the application reads data

OutputStreamAbstractionThe way the application writes data

  2. EOF = End, read-1 to the End.

  3. Basic input stream method (the following in indicates the object of the input stream)

Int B = in. read ();

Read an unsigned byte from the stream to the Lower 8 bits of the int. (An int variable is 4 bytes and 1 byte = 8 bits. The read () method can only read one byte and return an int, so this byte is filled to the lower eight bits of the int .) If the end is read,-1 is returned.

In. read (byte [] buf );

The data read from the stream is directly filled into the byte array buf. The number of bytes to be read depends on the length of the array.

In. read (byte [] buf, int start, int size );

Reads data from the stream to the byte array buf, and stores the size data starting from the start position of the buf.

  4. Basic Method of output stream (the following out indicates the object of output stream)

Out. write (int B );

Write a byte to the stream and write the low 8 bits of B.

Out. write (byte [] buf );

Write a byte array to the stream.

Out. write (byte [] buf, int start, int size );

Write Data of the size length to the stream starting from the start position of the buf.

  5. FileInputStream is a subclass of InputStream. It specifically implements Data Reading on the file:

Package test; import java. io. file; import java. io. fileInputStream; import java. io. inputStream; public class TestDemo {public static void main (String [] args) throws Exception {// we created a demo.txt on the d drive with the content "hello ". File file = new File ("D: \ demo.txt"); if (! File. exists () file. createNewFile (); InputStream in = new FileInputStream (file); int temp; while (temp = in. read ())! =-1) {System. out. print (Integer. toHexString (temp & 0xff) + "");} in. close (); System. out. println (); InputStream in2 = new FileInputStream (file); byte [] buf = new byte [1024]; in2.read (buf); String s = new String (buf, "GBK"); System. out. println (s); in2.close ();}}
TestDemo

Output: c4 e3 ba c3 68 65 6c 6c 6f
Hello

 

  

 

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.