Java 试图从CSV文件中删除一行时,我的代码会删除一行和下一行的一半

Java 试图从CSV文件中删除一行时,我的代码会删除一行和下一行的一半,java,Java,嗨,我需要能够删除CSV文件中的一行。我的代码当前删除一行,最多删除下一行的第一个逗号 我已尝试更改扫描仪定界符,但无法按需要使其正常工作 public void deleteRecord() { String filePath = "marks.txt"; System.out.println("Enter the StudentID of the record you wish to delete: "); Scanner in = new

嗨,我需要能够删除CSV文件中的一行。我的代码当前删除一行,最多删除下一行的第一个逗号

我已尝试更改扫描仪定界符,但无法按需要使其正常工作

public void deleteRecord() { 
        String filePath = "marks.txt";
        System.out.println("Enter the StudentID of the record you wish to delete: ");
        Scanner in = new Scanner(System.in);
        String removeTerm = in.nextLine();
        String tempFile = "temp.txt";
        File oldFile = new File(filePath);
        File newFile = new File(tempFile);

        try {
            FileWriter fw = new FileWriter(tempFile,true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            Scanner scan = new Scanner(oldFile);
            scan.useDelimiter("[,]");

            while (scan.hasNext()) {
                String ID = scan.next();
                String Mark = scan.next();
                if (!ID.equals(removeTerm)) {
                    pw.println(ID + "," + Mark);
                }
            }
            scan.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File(filePath);
            newFile.renameTo(dump);
            System.out.println("Record successfully deleted.");

        } catch(IOException e) {
            System.out.println("Error has occured.");

        }
    }
这是删除行之前的文件。 我将尝试删除第一行

43
B00343,59
B00653
25
B00757,31
B00876
40
B00421,62
B00568
78
B00826,79
B00126
93
B00862,62
B00999
12
B00237,68
B00762
85
B00864,49
事情就是这样

我需要它看起来像这样

B00567,43
B00343,59
B00653,25
B00757,31
B00876,40
B00421,62
B00568,78
B00826,79
B00126,93
B00862,62
B00999,12
B00237,68
B00762,85
B00864,49

有人能帮我解决这个问题吗?

您的输入文件每个记录只包含1个逗号,尽管您扫描了2次以获取ID和标记。因此,第一次扫描将显示ID“B00987”和标记“58” B00567'

您可以使用扫描仪并添加多个分隔符,如下所示:

s.useDelimiter(",|\\n");
这样,扫描器可以扫描到逗号和新行

不过,我不经常使用扫描仪,在您的情况下,我甚至更喜欢使用BufferedReader:

  public void deleteRecord() { 
        String filePath = "marks.txt";
        System.out.println("Enter the StudentID of the record you wish to delete: ");
        Scanner in = new Scanner(System.in);
        String removeTerm = in.nextLine();
        String tempFile = "temp.txt";
        File oldFile = new File(filePath);
        File newFile = new File(tempFile);

        try {
            FileWriter fw = new FileWriter(tempFile,true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            BufferedReader reader = new BufferedReader(new FileReader(oldFile);

            String line = "";
            while((line = reader.readLine()) != null) {
              if(!line.startsWith(removeTerm)) {
                pw.println(line);
              }
            }
            reader.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File(filePath);
            newFile.renameTo(dump);
            System.out.println("Record successfully deleted.");

        } catch(IOException e) {
            System.out.println("Error has occured.");

        }
    }

另外,使用autocloseables或至少正确管理资源将是一个很好的优化。

使用更多上下文和替代解决方案更改了我的原始注释。顺便说一句,对于实际工作(而不是家庭作业),我建议让库(例如)处理读/写CSV或制表符分隔文件的繁琐工作。
  public void deleteRecord() { 
        String filePath = "marks.txt";
        System.out.println("Enter the StudentID of the record you wish to delete: ");
        Scanner in = new Scanner(System.in);
        String removeTerm = in.nextLine();
        String tempFile = "temp.txt";
        File oldFile = new File(filePath);
        File newFile = new File(tempFile);

        try {
            FileWriter fw = new FileWriter(tempFile,true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            BufferedReader reader = new BufferedReader(new FileReader(oldFile);

            String line = "";
            while((line = reader.readLine()) != null) {
              if(!line.startsWith(removeTerm)) {
                pw.println(line);
              }
            }
            reader.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File(filePath);
            newFile.renameTo(dump);
            System.out.println("Record successfully deleted.");

        } catch(IOException e) {
            System.out.println("Error has occured.");

        }
    }