我试图写入一个文本文件,但即使该方法创建该文件,如果它不存在,它不会写.我已经通过其他几个有类似问题的帖子,并按照建议但没有运气.
通过使用调试器,String数据包含应该写入的正确数据,但它永远不会写入文本文件.
关于我忽略的事情的任何建议将不胜感激.
private static void createReservation(String filmName, String date, int noOfSeats, String username) {
FileWriter fw = null;
try {
File bookingFile = new File("C:\\server\\bookinginfo.txt");
if (!bookingFile.exists())
{
bookingFile.createNewFile();
}
fw = new FileWriter(bookingFile.getName(),true);
String data = "<"+filmName+"><"+date+"><"+Integer.toString(noOfSeats)+"><"+username+">\r\n";
fw.write(data);
fw.flush();
} catch (IOException ex) {
Logger.getLogger(FilmInfoHandler.class.getName()).log(Level.SEVERE, null, ex);
} finally {
try {
fw.close();
} catch (IOException ex) {
Logger.getLogger(FilmInfoHandler.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
Run Code Online (Sandbox Code Playgroud)