我有一个程序,允许用户将信息写入文件。它不会覆盖原始文件中的所有内容,而是添加到其中。问题是,如果用户没有输入足够的信息,程序就会崩溃。
文本文件中的每一行看起来都像这样:
info 1, Info 2, info 3, info 4, info 5, info 6, info 7
Every line has six commas and seven pieces of information. So, I need to know what kind of exception to use to prevent a crash if say the user only enters 4 pieces of information or only uses two commas, etc. Anybody know how to do that?
Here's my code:
private void addDVDButtonActionPerformed(java.awt.event.ActionEvent evt) {
FileWriter fWriter = null;
BufferedWriter writer = null;
try {
fWriter = new FileWriter("info.txt", true);
writer = new BufferedWriter(fWriter);
writer.write(JOptionPane.showInputDialog(this, "ADDING INFO"));
writer.newLine();
writer.close();
} catch (Exception e) {
JOptionPane.showMessageDialog(this,"More information required");
}
}
最佳答案
不要抛出异常。验证用户输入并仅在有效时将其写入文件。
关于java - FileWriter 使用什么异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5621080/