从csv读取,根据第一部分删除行

时间:2014-08-19 14:25:13

标签: java swing csv io

在我的应用程序中,我在JList中显示CSV的每一行的第一部分,当它被选中并按下按钮(删除)时,我希望它根据第一个条目从文件中删除该行。我正在尝试你有一个临时文件的方法,然后写入它然后在结束时重命名,但由于某种原因,这是不工作的。有什么想法吗?

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    // Delete service
    String selected = (String) jList1.getSelectedValue();
    File passwords = new File("/users/aak7133/desktop/passwords.txt");
    File temp = new File("/users/aak7133/desktop/temp.txt");

    try {
        BufferedReader reader = new BufferedReader(new FileReader(passwords));
        BufferedWriter writer = new BufferedWriter(new FileWriter(temp));
        String line;
        System.out.println(selected);
        while ((line = reader.readLine()) != null) {
            // trim newline when comparing with lineToRemove
            //String trimmedLine = line.trim();

            if (line.contains(selected)) {
                continue;
            }
            writer.write(line);
        }
        boolean successful = temp.renameTo(passwords);

    } catch (Exception e) {

    }
    updateList();
    clearFields();

} 

2 个答案:

答案 0 :(得分:1)

问题实际上是由开放的读者和作者引起的。这应该有效:

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
    String selected = (String) jList1.getSelectedValue();
    BufferedReader reader = null;
    BufferedWriter writer = null;
    try {
        File passwords = new File("/users/aak7133/desktop/passwords.txt");
        File temp = File.createTempFile("temp", ".txt", new File("/users/aak7133/desktop/"));
        reader = new BufferedReader(new FileReader(passwords));
        writer = new BufferedWriter(new FileWriter(temp));
        String line;
        System.out.println(selected);
        while ((line = reader.readLine()) != null) {
            // trim newline when comparing with lineToRemove
            // String trimmedLine = line.trim();

            if (line.contains(selected)) {
                continue;
            }
            writer.write(line + "\n");
        }
        if (passwords.canWrite()) {
            try {
                reader.close();
                reader = null;
            } catch (IOException ignore) {}

            try {
                writer.close();
                writer = null;
            } catch (IOException ignore) {}

            String path = passwords.getAbsolutePath();
            passwords.delete();
            boolean successful = temp.renameTo(new File(path));
            System.out.println(successful);

        }

    } catch (Exception e) {

    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException ignore) {}
        }
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException ignore) {}
        }
    }
     updateList();
     clearFields();
}

答案 1 :(得分:0)

我发现我需要在passwords.delete()之前加temp.renameTo(passwords)。这马上解决了这个问题。