1

是的,我已经搜索了通过比较和匹配删除特定行的方法,但是我的情况与我搜索的情况不同,并且发生了错误的操作。

这里的代码是actionPerformed按钮btnRemove。它的作用是从表格中删除选定的单元格,并且还意味着从文件中删除相应的单元格(字符串)值Activities.dat

但是,发生的情况是代码使整个文件被删除并变为空白,留下 1 个空行,而不仅仅是我想要删除的行。

btnRemove.addActionListener(new ActionListener() {

               public void actionPerformed(ActionEvent e)
               {

                    //Execute when button is pressed
                    int row = table.getSelectedRow(); 
                    int column = table.getSelectedColumn();

                    if (row >= 0) {
                        if (column >=0) {

                        for(int i=0; i<5; i++)
                         {
                            table.setValueAt("", row, column);     
                         }
                        }
                    }

                File file1 = new File("Activities.dat");
                try
                {
                    BufferedWriter writer = new BufferedWriter(new FileWriter(file1));
                    BufferedReader reader = new BufferedReader(new FileReader(file1));

                    Object lineToRemove = table.getValueAt(row, column);
                    lineToRemove.toString(); 
                    System.out.print(lineToRemove);

                    String currentLine;

                    while((currentLine = reader.readLine()) != null)
                    {
                    // trim newline when comparing with lineToRemove
                    String trimmedLine = currentLine.trim();

                    Object lineToRemove = table.getValueAt(row, column);
                    lineToRemove.toString();                                        

                    if(trimmedLine.equals(lineToRemove)) continue;
                        writer.write(currentLine);
                    }                                                               
                     writer.close();

                }
                catch (IOException ex)
                {
                    Logger.getLogger(ActivityScreen.class.getName()).log(Level.SEVERE, null, ex);
                }
               }
           }); 
4

2 回答 2

1

看看RandomAccessFile。将文件保持原样,直到要删除的行,然后将文件的其余部分从已删除部分的末尾复制到文件末尾,从您开始删除的点开始。

伪代码:

String line = "line I want to remove";
RandomAccessFile raf = openRandomAccessFile();

while(!line.equals(raf.readLine())) {
    //just going to the point we want
}

long beginOfTheRemovingPointer = raf.getFilePointer()-size of removed line;
raf.seek(beginOfTheRemovingPointer);
raf.write(from beginOfTheRemovingPointer+size of removed line until the end);
raf.setLength(new length);

RandomAccessFile方法是存在的。只需查看 API 即可了解它们的具体功能。

于 2013-03-12T13:29:32.353 回答
1

您正在循环lineToRemove中制作副本。如果您更改了to的类型并将其值分配给它(并且之后不要更改它),while那也会更好。此外,您正在设置 [ , ]的值,然后再获得它。您将搜索(如果不是一个非常好的主意,则什么也不搜索)。尝试做这样的事情:lineStringtable.getValueAt(row, column).toString()rowcolumn""

    btnRemove.addActionListener(new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            // Execute when button is pressed
            int row = table.getSelectedRow();
            int column = table.getSelectedColumn();
            if (row >= 0) {
                if (column >= 0) {
                    File file1 = new File("Activities.dat");
                    File temp = new File(file1.getAbsolutePath() + ".tmp"); // temp file
                    PrintWriter writer = null;
                    BufferedReader reader = null;
                    try {
                        writer = new PrintWriter(new BufferedWriter(new FileWriter(temp)), false);
                        reader = new BufferedReader(new FileReader(file1));
                        String lineToRemove = table.getValueAt(row, column).toString();
                        System.out.print(lineToRemove);
                        String currentLine;
                        while ((currentLine = reader.readLine()) != null) {
                            // trim newline when comparing with lineToRemove
                            String trimmedLine = currentLine.trim();
                            if (trimmedLine.equals(lineToRemove))
                                continue;
                            writer.println(currentLine);
                        }
                    } catch (IOException ex) {
                        Logger.getLogger(ActivityScreen.class.getName()).log(Level.SEVERE, null, ex);
                    } finally {
                        try {
                            if (reader != null)
                                reader.close();
                            if (writer != null) {
                                writer.flush();
                                writer.close();
                                file1.delete(); // delete the old one
                                temp.renameTo(file1); // make temp same as original
                            }
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }
                    }
                    table.setValueAt("", row, column);
                }
            }
        }
    });
于 2013-03-12T13:58:42.557 回答