java - FileWriter 使用什么异常

标签 java file exception filewriter writer

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

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

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/

相关文章:

java - 替换选择排序 :

php - 使用 PHP 列出 .txt 文件中的项目

file - Haskell - 将包含数字的文件读入列表

java - 如何在 Java EE 环境中管理数据库和文件系统的事务?

Java jackson : deserialize complex polymorphic object model: JsonMappingException: Unexpected token (START_OBJECT), 应为 VALUE_STRING

java - Java 中相同的密码会产生不同的 AES key

java - 获取swt表中单元格的值

java - hibernate 无法与 Heroku postgresql 插件连接

Javascript eval() 异常 - 行号

exception - 奇数异常术语 "throwing up"