日期:2014-05-20  浏览次数:20794 次

菜鸟求解问题出在哪里,谢谢
[code=Java][/code]
import java.io.*;
public class ReaderTest
{
public static void main(String[] args)
{
//声明一个字符流;
FileReader fid = null;
//声明一个字符数组,来存字符;
char[] buff = new char[32];
//来记录返回的字节个数
int hasRead = 0;
try
{
fid = new FileReader("ReaderTest.java");
//循环读取内容
while((hasRead = fid.read(buff)) > 0);
{
System.out.print(new String(buff,0,hasRead));
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
得到的结果:Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String
ex out of range: -1
  at java.lang.String.<init>(String.java:208)
  at ReaderTest.main(ReaderTest.java:18)
问题:1为啥把32换成1024就行了?2还有前提32换成1024,然后把System.out.print(new String(buff,0,hasRead));
换成System.out.print(buff);也能一样输出来,那用String有啥意义么?
求教求教

------解决方案--------------------

while((hasRead = fid.read(buff)) > 0);

多了个分号,晕死了,写代码仔细一点