FileWriter 使用什么异常
我有一个程序,允许用户将信息写入文件。它不会覆盖原始文件中的所有内容,而是添加到其中。问题是,如果用户没有输入足够的信息,程序就会崩溃。
文本文件中的每一行看起来像这样:
info 1, Info 2, info 3, info 4, info 5, info 6, info 7
每行有六个逗号和七个信息。所以,如果用户只输入 4 条信息或只使用两个逗号等,我需要知道使用哪种异常来防止崩溃。有人知道该怎么做吗?
这是我的代码:
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");
}
}
I have a program that allows a user to write information to a file. It doesn't write over everything in the original file, but adds to it. The thing is if the user doesn't enter enough info the programs crashes.
Each line in the text file looks something like this:
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");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不要抛出异常。验证用户输入并仅在有效时将其写入文件。
Do not throw an exception. Validate the user input and only write it to the file if it is valid.
例如,使用
String.split(",")
将为您提供一个String[]
,您可以在其中检查String[].length = = 7 。
编辑:只是为了确保这不是您问题的完整解决方案,而是一个很好的起点。我强烈同意所有其他评论/答案,抛出异常是矫枉过正的。
编辑 2: 假设用户提供了以下字符串作为输入,并且您希望在写入文件之前对其进行验证:
String str = "a1,b2,c3,d4 ,e5,f6";
如您所见,该字符串有六个元素和五个逗号,换句话说,它应该向用户发出警告。检查这一点的一个非常简单的方法是:
请注意,这只是显示了原理,在您的情况下,您可能(并且可能会)想要检查其他内容,例如格式,或者您有字母数字文本且没有下划线或其他内容。如果您想要检查很多条件,您可以编写一个方法
validateInput(str)
,它可以是一系列条件检查,并且仅当所有条件都满足时才返回true
条件检查。For example by using
String.split(",")
which will give you aString[]
, on which you can check ifString[].length == 7
.edit: just to make sure this is not a complete solution to your problem, but a good starting point. I strongly agree with all the other comments/answers, throwing an exception is overkill.
edit 2: Let's assume that the user gave the following string as an input, and you want to validate it before writing out to the file:
String str = "a1,b2,c3,d4,e5,f6";
as you can see this string has six elements and five commas, in other words it should give a warning to the user. A very simple way of checking this would be:
note that this just shows the principle, in your case you might (and probably will) want to check for other things like formatting, or that you have alpha-numeric text and no underscores or whatever. If you have a lot of conditions you want to check for, you can write a method
validateInput(str)
which could be a series of condition checks and returnstrue
only if all conditions check out.