请问这个java.lang.RuntimeException异常原因是什么

qq_33679861 2017-02-24 02:17:25
执行以下代码时,提示Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.RuntimeException: big.txt not found!!!
java菜鸟,请问谁能指点一下这个异常的原因呢,big.txt是存在的啊。
public static Map<String,Integer> train(){
[color=#FF0000]InputStream is = new SpellCorrect().getClass().getClassLoader().getResourceAsStream("big.txt");
if(is == null){
throw new RuntimeException("big.txt not found!!!");
}
Map<String,Integer> map = new HashMap<String,Integer>();
[/color]
try {
//读取语料库big.txt
BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"), 512);
String s="";
while ((s = br.readLine()) != null) {
// 去掉文档中除字母外的所有符号
s = s.replaceAll("\\pP|\\pS|\\pM|\\pN|\\pC", "");
// 将文档转成小写,然后切分成单词,存在list中
s = s.toLowerCase();
String[] splits = s.split(" ");
for (int j = 0; j < splits.length; j++) {
if (!" ".equals(splits[j]) && !"".equals(splits[j]) && !splits[j].equals(null)){
if(map.containsKey(splits[j])){
Integer count=map.get(splits[j]);
map.put(splits[j], count+1);
}else{
map.put(splits[j], 1);
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
finally{
try{
is.close();
}catch(Exception e){
e.printStackTrace();
}
}
return map;
}
...全文
5251 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_33679861 2017-02-27
  • 打赏
  • 举报
回复
引用 3 楼 qnmdcsdn 的回复:
放到包的最外层试试
可以了 谢谢
TrineaShao 2017-02-26
  • 打赏
  • 举报
回复
big.txt资源没被发现,文件路径引用错误。类无法加载到此资源
javasishen 2017-02-24
  • 打赏
  • 举报
回复
路径不对,楼上正解,InputStream is = SpellCorrect.class.getResourceAsStream(“big.txt”);
bcsflilong 2017-02-24
  • 打赏
  • 举报
回复


InputStream is = SpellCorrect.class.getResourceAsStream(“big.txt”);
  • 打赏
  • 举报
回复
放到包的最外层试试
qq_33679861 2017-02-24
  • 打赏
  • 举报
回复
引用 1 楼 qnmdcsdn 的回复:
路径不对吧。


txt文件不是应该放在这里吗?
  • 打赏
  • 举报
回复
路径不对吧。

58,452

社区成员

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

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