Both of these methods exist as abstract methods in the abstract class InputStream ,
The JDK API describes both of these:
read (): &NBSP reads the next byte of data from the input stream, returning 0 to 255 in the range of int byte value. If there are no bytes available since the end of the stream has been reached, -1 is returned. This method is blocked until the input data is available, the end of the stream is detected, or an exception is thrown.
Read (byte[] b): reads a certain number of bytes from the input stream and stores it in buffer array B. Returns the number of bytes actually read in integer form. This method is 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 an attempt to read at least one byte is attempted. If no bytes are available because the stream is at the end of the file, a value of 1 is returned, or at least one byte is read and stored in B.
The first byte to be read is stored in the element b[0], the next one is stored in b[1], and so on. The number of bytes read is at most equal to the length of B. Set K to the actual number of bytes read, these bytes will be stored in the elements of b[0] to b[K-1], without affecting the elements b[K] to b[b.length-1].
As explained in the Help documentation, the read () method can read only one byte at a time, so it can read only some characters that are within the ASCII code range. These characters are primarily used to display modern English and other Western European languages. The characters in Unicode, such as kanji, are not read normally. Can only be displayed in garbled form.
For the above disadvantages of the read () method, in read (byte[] b) is solved, take the Chinese character to give an example, a Chinese character occupies two bytes, you can define the parameter array B as an array of size 2 to read the Chinese characters normally. Of course B can also be defined as larger, such as if B=new byte[4], you can read two characters each time, but it should be noted that if the size of B is defined as 3 or 7 odd, then for all of the characters of a document can not be read and write all normal.
Here's an example to illustrate the use of both:
Example description: Class Inputstreamtest.java to demonstrate the use of the Read () method. Class Inputstreamtest1.java to demonstrate the use of read (byte[] b). The main task of the two classes is to read the contents of the text document Yhw.txt through the file input stream FileInputStream and output to the console display.
Check it out first. Yhw.txt the contents of the document:
Inputstreamtest.java The code is as follows:
run Result: 3.1415926 No pains,No gains!! (garbled)
inputstreamtest1.java Code as follows:
Operation Result:
3.1415926 No Pains,no gains!! The most is that a bow of gentleness, like a water lotus shy cool breeze.
The difference between the three read () methods of a InputStream
1.read
This method is to read the stream one byte at a time, and the returned int is the int representation of the byte.
Here is the code snippet, tested when Eclipse's encoding is GBK, the converted string does not have to be re-encoded, and if Eclipse's encoding is utf-8, the conversion from byte to string needs to be utf-8.
InputStream in = Test.class.getResourceAsStream ("/tt.txt"); byte[]tt=new byte[15];//test beforehand know there are 15 bytecode while ( In.available ()!=0) {for (int i=0;i<15;i++) {tt[i]= (byte) in.read (); }}string ttttt=new String (TT, "Utf-8"); System.out.println (TTTTT); In.close ();
2.read(byte[] b)
This method is to specify an array length, buffer the bytes in the stream into array B, the number of bytes in the array returned, the buffer is not full, then return the true number of bytes, to the end of the return 1
in = Test.class.getResourceAsStream ("/tt.txt"); byte [] tt=new byte[1024];int B;while ((B=in.read (TT))!=-1) {System.out . println (b); String Tzt=new string (TT, "Utf-8"); System.out.println (tzt);}
3.read(byte[] b, int off, int len)
This method actually calls the read () method multiple times
InputStream in = Test.class.getResourceAsStream ("/tt.txt"); System.out.println (In.available ());//This method is to return the number of bytes in this stream, the length of the array can be set to this byte[]tt=new byte[in.available ()]; int z; while ((Z=in.read (TT, 0, tt.length))!=-1) {System.out.println (new String (TT, "Utf-8")); }
Java Learning InputStream in read () and read (byte[] b)