FileInputStream read two methods: the read byte by byte; byte array to read

1: read (): 
Reads the next byte of data from the input stream, the byte int return value in the range 0 to 255. If the end of the stream has been reached because no byte is available, it returns -1. Before the input data is available, detecting the end of the stream or throw an exception, this method has been blocked. 

For InputStream.read () This method is a time from the stream reading a byte read, the efficiency is very low. A better way is to use InputStream.read (byte [] b), or InputStream.read (byte [] b, int off, int len) method, a plurality of bytes read.

2: read (byte [] b   ):
read a certain number of bytes from the input stream, and stores it in the buffer array b. It returns the number of bytes actually read as an integer. Before the input data is available, end of file is detected, or an exception is thrown, this method has been blocked. If the length of b is 0, no bytes are read and 0 is returned; otherwise, attempt to read at least one byte. If the end of the file because the stream is not available byte, the value -1; otherwise, at least one byte is read and stored into b. In the element b [0], the b [1], the next and so on stored in the first byte read is stored. Number of bytes read at most equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in b [0] to b [k-1] in the element does not affect b [k] to the element b [b.length-1] is. 

read () method reads only one byte, so it can only be read by a number of characters in the ASCII code range. These characters are primarily used to display modern English and other Western European languages. For Chinese characters unicode characters can not be read properly. It displayed only in garbled form.

3. With regard to available InputStream class () method
        for reading bytes once, InputStream.available is often used () method, which can read and write operations before the first data stream that number of bytes that can be read. Note that if this method is used in this from
reading the data to a file, generally do not have problems.

   But if it is used for network operations, often encounter some trouble. For example, when Socket Communications, the other obviously sent a 1000 bytes, but his program calls available () method received only 900, or 100, or even zero, feeling a little puzzled, how could not find the cause. In fact, this is because the network communications are often intermittent, often a string of bytes sent in several batches. Local program call available () method sometimes get 0, this may be the other side has not responded, it could be the other party has responded, but the data has not delivered locally. Other party to send 1,000 bytes to you, maybe divided into three batches arrive, you have to call it 3 times available () method to get the total number of all data.

If you write code like this:
  int = in.available COUNT ();
  byte [] b = new new byte [COUNT];
  in.read (b);
      when the network is often an error during operation, because you call available () method, send data sent may not arrive, you get the count is 0.
         We need to change this:
  int COUNT = 0;
  the while (COUNT == 0) {
   COUNT = in.available ();
  }
  byte [] = new new byte B [COUNT];
  in.read (B);

 

4. About InputStream.read (byte [] b), and InputStream.read (byte [] b, int off, int len) these two methods are used to read a plurality of bytes from the stream, there is experienced programmers will find that these two methods often can not read the number of bytes you want to read. The first such method, application programmers often want to be able to read b.length bytes, but the reality is that the system often can not read so much. Carefully read the instructions on the Java API discovered, this method is not guaranteed to be read so many bytes, so it can only read up to ensure multiple bytes (at least one). Thus, if you want the program reads count bytes, preferably with the following code:
  byte [] = new new byte B [count];
  int ReadCount = 0; // number of bytes successfully read
  while (readCount <count) {
   ReadCount + = in.read (bytes, ReadCount, count - ReadCount);
  }
      with this code ensures that read count bytes IO exception encountered unless the middle or end of the data stream to the (EOFException)

 

Read the contents of the file:

 

 

 Solve the garbage:

   // InputStreamReader (InputStreamis): read the data encoded with the default;

     // InputStreamReader (InputStreamis, StringcharsetName): reads the specified encoded data 

Guess you like

Origin www.cnblogs.com/renjiaqi/p/11427520.html