代码:import java.io.*;
public class FileApp {
  public static void main(String[] args){
  byte[] buffer=new byte[25];
  try{
  FileInputStream fileIn = new FileInputStream("1.txt");
  int bytes=fileIn.read(buffer);
  System.out.println(bytes);
  }
  catch (Exception e){
  System.out.println(e);
  }
  }
}1.txt内容为“abc12345678”,read方法以字节数组为参数,结果返回24。把字节数组buffer的长度设为25以上,返回还是24。只有小于25时,返回的值和字节数组的长度才一致。read方法以字节数组为参数时,返回的整形值到底代表什么意思呢?

解决方案 »

  1.   

    public int read(byte[] b)
             throws IOException从此输入流中将最多 b.length 个字节的数据读入一个字节数组中。在某些输入可用之前,此方法将阻塞。 覆盖:
    类 InputStream 中的 read
    参数:
    b - 存储读取数据的缓冲区。 
    返回:
    读入缓冲区的字节总数,如果因为已经到达文件末尾而没有更多的数据,则返回 -1。 
    抛出: 
    IOException - 如果发生 I/O 错误。
    另请参见:
    InputStream.read(byte[], int, int)
      

  2.   

    明白了。
    刚才我使用的文本文件是unicode编码,所以返回24。改成ansi编码,返回11。正确了。