FileWriter 使用什么异常

发布于 2024-10-31 07:32:41 字数 833 浏览 0 评论 0原文

我有一个程序,允许用户将信息写入文件。它不会覆盖原始文件中的所有内容,而是添加到其中。问题是,如果用户没有输入足够的信息,程序就会崩溃。

文本文件中的每一行看起来像这样:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

放我走吧 2024-11-07 07:32:41

不要抛出异常。验证用户输入并仅在有效时将其写入文件。

Do not throw an exception. Validate the user input and only write it to the file if it is valid.

吹梦到西洲 2024-11-07 07:32:41

我该如何验证呢?这就是我所坚持的。

例如,使用 String.split(",") 将为您提供一个 String[],您可以在其中检查 String[].length = = 7 。

编辑:只是为了确保这不是您问题的完整解决方案,而是一个很好的起点。我强烈同意所有其他评论/答案,抛出异常是矫枉过正的。

编辑 2: 假设用户提供了以下字符串作为输入,并且您希望在写入文件之前对其进行验证:

String str = "a1,b2,c3,d4 ,e5,f6";

如您所见,该字符串有六个元素和五个逗号,换句话说,它应该向用户发出警告。检查这一点的一个非常简单的方法是:

if (str.split(",").length != 7) // checks if the string is comma delimited and has seven pieces of information
    warnUser();   // whatever you want to do to warn user
else 
    writeToFile(str);

请注意,这只是显示了原理,在您的情况下,您可能(并且可能会)想要检查其他内容,例如格式,或者您有字母数字文本且没有下划线或其他内容。如果您想要检查很多条件,您可以编写一个方法 validateInput(str),它可以是一系列条件检查,并且仅当所有条件都满足时才返回 true条件检查。

How would I verify it though? This is what I am stuck on.

For example by using String.split(",") which will give you a String[], on which you can check if String[].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:

if (str.split(",").length != 7) // checks if the string is comma delimited and has seven pieces of information
    warnUser();   // whatever you want to do to warn user
else 
    writeToFile(str);

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 returns true only if all conditions check out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文