// 初始化阶段,读取new_words.txt
// web工程中读取 文件,必须使用绝对磁盘路径
String path = getServletContext().getRealPath("/WEB-INF/new_words.txt");
System.out.println(path);
// path = "C:\\Users\\admin\\IdeaProjects\\webDemo\\firstWeb\\web\\WEB-INF\\new_words.txt ";
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
words.add(line);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
- 上面这个代码一直出错,我不知道原因,后面不停的查找资料发现了问题:我的web项目输出目录有问题,因为我也是第一次用idea写web项目,发现很多bug
- 其实都没有太大问题,我们需要注意的是你必须在out这个输出文件夹中拥有这个文件,out输出文件夹会默认过滤这些文件