[英]Text File Writing Not Working
我一直在嘗試寫入文本文件而不是System.out.println()
進行輸出。 但是,當我嘗試此操作時,似乎什么也沒寫。 我的代碼有什么問題?
try{
List<String> lines = Arrays.asList("Data Goes Here");
Path file = Paths.get("output.txt");
Files.write(file, lines, Charset.forName("UTF-8"));
}
catch (IOException ex) {
System.out.println("Frick, something broke. Sorry folks, go home.");
}
我只是通過將路徑作為位於我項目根目錄中的resources目錄的路徑來對您的代碼進行了少量更改。 我能夠成功寫入文件。
這是更新的代碼:
try {
List<String> lines = Arrays.asList("Data Goes Here");
Path file = Paths.get("./resources/test.txt");
Files.write(file, lines, Charset.forName("UTF-8"));
}
catch (IOException ex) {
ex.printStackTrace();
System.out.println("Frick, something broke. Sorry folks, go home.");
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.