Operation of the Java 20-7-byte input stream

Source: Internet
Author: User

 Byte input stream operation steps:
A: Create A Byte input stream object
  B: Call the Read () method and display the data in the console
C: Releasing Resources

Step A, C skip, say step b

How to read data :
A:int Read (): read one byte at a time
B:int Read (byte[] b): read one byte array at a time

To do the byte input stream, I have to create a file and output the content to this file with a byte output stream:

1 // First I create a TXT file and output the content 2         New File ("A.txt"); 3         4         // output data with byte output stream 5         New fileoutputstream (file); 6             Out.write ("Java IO". GetBytes ()); 7             Out.close ();

How to read Data A:int read (): read one byte at a time

int by = Fis.read (); System.out.println (by); // The output here will be the ASCII code ' 106 ' of J. System.out.println ((char) by); // so the type conversion, output ' J ', because each read a byte, will be converted type, so if it is a Chinese character, then we can not understand the conversion results

Because this method simply reads a byte, if you continue to write it, it will output the next byte, which is equivalent to adding a next in front.

Until you read the content and finally, there is no content to read, it returns "1". So, we can use loops to output the contents of the content at once:

int by = 0and (by = In.read ())! =-1) {// when the return is-1, the representation is not behind the content, jump out of the loop. System.out.print ((char) by);

How to read data B:int read (byte[] b): read one byte array at a time

The length of the array is usually 1024 or 1024 of the integer multiples

The return value of this method is actually the number of bytes actually read. Similarly, if the return value of this method is "1", then there is no data behind

1 byte New byte [the integer multiples of the 1024x768];//1024 2 int len = 0;//Store the actual length of the read 3 while (len = In.read (bys))! =-1) {4
     System.out.print (new String (bys, 0, Len));//Print all the contents of this array 5 }

Note: For convenience, the above method throws the exception. Remember, be sure to release the resources

Xxx.close ();

Operation of the Java 20-7-byte input 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.