小弟是java新手,最近正在研究文件的读取,偶尔看到这么一段代码:
void openFile(String filename)
{
try{
File file=new File(filename);
int size =(int)file.length();
int chars_read=0;
FileReader int=new FileReader(file);
char[] data=new char [size];
while(in.ready())
{char_read+=in.read(data,chars_read,size-chars_read);}   }
in.close()
}请问in.ready()这个方法是什么意思啊,怎么个用法,从来没见过啊?
size中保存的是要打开的文件的长度,我认为只用一次in.read(data,chars_read,size-chars_read)就可把整个文件读入数组中,为什么还要加个while循环啊?请各位朋友赐教!!!!
    谢谢

解决方案 »

  1.   

    程序的第十行应该是chars_read而不是char_read 更正一下子,我将异常处理给省了,别的没有问题了,运行是没有问题的
      

  2.   

    ready
    public boolean ready()
                  throws IOException
    Tell whether this stream is ready to be read. An InputStreamReader is ready if its input buffer is not empty, or if bytes are available to be read from the underlying byte stream. Overrides:
    ready in class Reader
    Returns:
    True if the next read() is guaranteed not to block for input, false otherwise. Note that returning false does not guarantee that the next read will block. 
    Throws: 
    IOException - If an I/O error occurs