// 初始化階段,讀取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輸出文件夾會默認過濾這些文件