为什么每次我使用“[\n]”分隔符追加时都会不断出现新行?

Why do new lines keep appearing every time I append using "[,\n]" delimiter?

我是 java 编程的新手,我目前正在构建一个程序,通过文件处理来更新用户的信息。

例如,我最初将其保存在记事本中。

Milo,10,India
Jacob,15,California
Shan,7,France

我想换个国家。所以我初始化了一个扫描器来读取文件中的每一行,并使用分隔符(“[,\n]”)。我能够更改它,但每次我进行任何更改时,每行之后都会出现新行。

我每次追加的输出是这样的:新行随机出现,我不知道为什么。

Milo,10,Italy
Jacob,15,California



Shan,7,France

这是我的代码:

private static Scanner x;
    
    public static void main(String[] args) throws IOException {
        File file = new File("rewrite.txt");
        String nameString = "Milo";
        String newcountry = "Italy";
        
        String tempFile = "temp.txt";
        File oldFile = new File("rewrite.txt");
        File newFile = new File(tempFile);
        String name = ""; 
        String age = "";
        String country = "";
        
        try {
            FileWriter fw = new FileWriter(tempFile, true);
            BufferedWriter bw = new BufferedWriter(fw);
            PrintWriter pw = new PrintWriter(bw);
            x = new Scanner(new File("rewrite.txt"));
            x.useDelimiter("[,\n]");
            
            while(x.hasNext()) {
                name = x.next();
                age = x.next();
                country = x.next();
                
                if (name.equals(nameString)) {
                    
                    pw.println(name + "," + age + "," + newcountry);
                    System.out.println(name + "," + age + "," + newcountry);
                
                } else {
                    pw.println(name + "," + age + "," + country);
                    System.out.println(name + "," + age + "," + country);
                }
            }
            x.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File("rewrite.txt");
            newFile.renameTo(dump);
            
        } catch (Exception e) {
            // TODO: handle exception
        }

阅读整行,以逗号分隔。然后将值赋给变量并写入新文件。

public static void main(String[] args) {
    File input = new File("C:\temp\input.txt");
    String nameString = "Milo";
    String newcountry = "Italy";

    File output = new File("c:\temp\temp.txt");
    String name = "";
    String age = "";
    String country = "";
    try {
        FileWriter fw = new FileWriter(output, true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(bw);
        Scanner x = new Scanner(input);
        while (x.hasNextLine()) {
            String line = x.nextLine();
            String[] split = line.split(",");
            name = split[0];
            age = split[1];
            country = split[2];
            if (name.equals(nameString)) {
                pw.println(name + "," + age + "," + newcountry);
                System.out.println(name + "," + age + "," + newcountry);
            } else {
                pw.println(name + "," + age + "," + country);
                System.out.println(name + "," + age + "," + country);
            }
        }
        x.close();
        pw.flush();
        pw.close();

    } catch (Exception e) {
        // TODO: handle exception
    }
}

输出为:

Milo,10,Italy
Jacob,15,California
Shan,7,France

没有任何空行。

经过一些调试,我发现了您遇到的问题。您只使用部分新行分隔符:

Text files created on DOS/Windows machines have different line endings than files created on Unix/Linux. DOS uses carriage return and line feed ("\r\n") as a line ending, which Unix uses just line feed ("\n")

当更新分隔符以也使用 \r 时,效果一样好

x.useDelimiter(",|\r\n");

这告诉扫描器使用 ,\r\n 作为分隔符,它使用 windows 换行符