求助
一个简单的io流问题,btn_show为显示按钮,单击按钮输入指定内容,但是在读的时候遇到下面问题,求解。
btn_show.addActionListener(new ActionListener(){
byte[]data = new byte[10];
FileInputStream in = null;
public void actionPerformed(ActionEvent e) {
StringBuffer sb = new StringBuffer();
try {
in = new FileInputStream("F:\\io\\date.txt");
try {
int count = -1;
while((count=in.read(data)) != -1) {
sb.append(new String(data,0,count));
}
txt_show.setText(sb.toString());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
上段为正常代码,运行没有问题。
btn_show.addActionListener(new ActionListener(){
StringBuffer sb = new StringBuffer();
public void actionPerformed(ActionEvent e) {
FileInputStream in = null;
byte []b = new byte[10];
try {
in = new FileInputStream("F:\\io\\date.txt");
int count = -1;
while((count = in.read(b)) != -1);{
sb.append(new String(b,0,count));
}
txt_show.setText(sb.toString());
}
catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}finally {
try {
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
});
此段为问题代码,运行报“字符串下标越界”异常。
我仔细看了一个多小时,就是没看出差别,但是下部分的代码运行就是有问题,求解答迷惑。