file input error

coderone 2008-03-17 08:48:05
下面的两个功能对吗?为什么老是进入catch ()里?? (源代码太长)

try{
//计算行数
File fis = new File ("C:\\myfile.txt");
BufferedReader dis = new BufferedReader(fis);
while(dis.readLine()!=null){
enter_count++;
}
//将每一行符值给一个字符串
fis = new FileInputStream("C:\\myfile.txt");//由于上面的dis.readLine()将文件读完了,所以要从新初始化一遍
dis = new DataInputStream(fis);
String s[] = new String[enter_count];
i = 0;
while(dis.available()!=0){
s[i] = dis.readLine();
s1[i]=s[i];
i++;
}
dis.close();
}catch(Exception e){
System.err.println("File input error");
}
}
}
...全文
67 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
yriio 2008-03-17
  • 打赏
  • 举报
回复
上下两段分开来try/catch一下试试看...


int enter_count = 0;
try {
// 计算行数
FileInputStream fis = new FileInputStream("abc.txt");
BufferedReader dis = new BufferedReader(new InputStreamReader(fis));
while (dis.readLine() != null) {
enter_count++;
}
System.out.println(enter_count);
// 将每一行符值给一个字符串
fis = new FileInputStream("abc.txt");// 由于上面的dis.readLine()将文件读完了,所以要从新初始化一遍
DataInputStream ds = new DataInputStream(fis);
String s[] = new String[enter_count];
int i = 0;
while (ds.available() != 0) {
System.out.println(ds.readLine());
}
dis.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println("File input error");
}

这样是可以的...没错啊...

语法错误吧? @_@

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧