In inputstream, read () and read (byte [] B)

Source: Internet
Author: User

 

The read () and read (byte [] B) methods exist as abstract methods in the abstract class inputstream. The latter is not. The JDK API describes the two methods as follows:

1: Read ():
Reads the next byte of data from the input stream, and returns the int byte value ranging from 0 to 255. -1 is returned if no Bytes are available because it has reached the end of the stream. This method is blocked until the input data is available, the end of the stream is detected, or an exception is thrown.

2: Read (byte [] B ):
Read a certain number of bytes from the input stream and store them in the buffer array B. Returns the actual number of bytes read as an integer. This method is always blocked until the input data is available, the end of the file is detected, or an exception is thrown. If the length of B is 0, no Bytes are read and 0 is returned. Otherwise, at least one byte is read. If the stream is at the end of the file and there are no available bytes, the returned value is-1; otherwise, at least one byte is read and stored in B. Store the first byte read in element B [0], the next byte in B [1], and so on. The maximum number of bytes read is equal to the length of B. Set K to the number of bytes actually read; these bytes are stored in the elements of B [0] to B [k-1], without affecting B [k] to B [B. length-1.

According to the explanation in the help document, the read () method can only read one byte at a time, so it can only read some characters within the ASCII code range. These characters are mainly used to display modern English and other Western European languages. Unicode characters such as Chinese characters cannot be read normally. It can only be displayed in garbled format.

The preceding disadvantages of the read () method are solved in read (byte [] B). For example, a Chinese character occupies two bytes, you can define parameter array B as an array of 2 to read Chinese characters. Of course, B can also be defined as larger. For example, if B = new byte [4], two Chinese characters can be read at a time, but note that, if the size of B is defined as an odd number such as 3 or 7, a document that is full of Chinese characters cannot be fully read and written.

 

Inputstreamtest2.java 

Java code
  1. /**
  2. * User: liuwentao
  3. * Time: 12-1-25 AM
  4. */
  5. Public class inputstreamtest2 {
  6. Public static void main (string [] ARGs ){
  7. String Path = "d :\\ project \ opensouce \ opensouce_demo \ base_java \ SRC \ demo \ Java \ inputstream \\";
  8. File file = new file (path + "xuzhimo.txt ");
  9. Inputstream = NULL;
  10. Int I = 0;
  11. Try {
  12. Inputstream = new fileinputstream (File );
  13. Byte [] bytes = new byte [16];
  14. While (I = inputstream. Read (bytes ))! =-1 ){
  15. String STR = new string (bytes );
  16. System. Out. Print (STR );
  17. }
  18. } Catch (filenotfoundexception e ){
  19. E. printstacktrace ();
  20. } Catch (ioexception e ){
  21. E. printstacktrace ();
  22. }
  23. }
  24. }


Execution result:

 

Unfortunately, there are still garbled characters. For the solution, refer to the following tutorial.
Http://wentao365.iteye.com/blog/1183951

Modified code:

Java code
  1. /**
  2. * User: liuwentao
  3. * Time: 12-1-25 AM
  4. */
  5. Public class inputstreamtest3 {
  6. Public static void main (string [] ARGs ){
  7. String Path = "d :\\ project \ opensouce \ opensouce_demo \ base_java \ SRC \ demo \ Java \ inputstream \\";
  8. File file = new file (path + "xuzhimo.txt ");
  9. Inputstream = NULL;
  10. String line;
  11. Stringbuffer = new stringbuffer ();
  12. Try {
  13. // Inputstream: 1) abstract class, 2) bytes-oriented I/O operations (eight-byte streams ).
  14. Inputstream = new fileinputstream (File );
  15. // Reader: 1) abstract class, 2) character-oriented I/O operations (16-bit Unicode characters ).
  16. Reader reader = new inputstreamreader (inputstream, "UTF-8 ");
  17. // Added the buffer function.
  18. Bufferedreader = new bufferedreader (Reader );
  19. While (line = bufferedreader. Readline ())! = NULL ){
  20. Stringbuffer. append (line );
  21. }
  22. If (bufferedreader! = NULL ){
  23. Bufferedreader. Close ();
  24. }
  25. String content = stringbuffer. tostring ();
  26. System. Out. Print (content );
  27. } Catch (filenotfoundexception e ){
  28. E. printstacktrace ();
  29. } Catch (ioexception e ){
  30. E. printstacktrace ();
  31. }
  32. }
  33. }


Execution result:



Sorry, there is no line feed.

Solution: Use commons-io-*. Jar

Java code
  1. /**
  2. * User: liuwentao
  3. * Time: 12-1-25 AM
  4. */
  5. Public class inputstreamtest4 {
  6. Public static void main (string [] ARGs ){
  7. String Path = "d :\\ project \ opensouce \ opensouce_demo \ base_java \ SRC \ demo \ Java \ inputstream \\";
  8. File file = new file (path + "xuzhimo.txt ");
  9. String content = NULL;
  10. Try {
  11. Content = fileutils. readfiletostring (file, "UTF-8 ");
  12. } Catch (ioexception e ){
  13. E. printstacktrace ();
  14. }
  15. System. Out. println ("content:" + content );
  16. }
  17. }


Execution result:

 

Http://wentao365.iteye.com/blog/1374731

 

In inputstream, read () and read (byte [] B) (convert)

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.