@@@@@@@@@@@@@@文件的读取
小弟是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循环啊?
请各位朋友赐教!!!!
谢谢
------解决方案--------------------好像有错误啊!
------解决方案--------------------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
------解决方案--------------------程序不对头呀