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

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

提问人:Baek Ga Eun 提问时间:4/29/2022 最后编辑:user207421Baek Ga Eun 更新时间:4/29/2022 访问量:69

问:

我是 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
        }
Java java.util.scanner 分隔符

评论

2赞 user207421 4/29/2022
这不是真正的代码。此代码不会生成此输出。您一遍又一遍地进行比较:这从来都不是真的,因此您从未进行此处所示的国家/地区替换。注意:作为一个风格点,你已经建造了三次。你可以用一个。之前也是多余的。"Jacob"""new File("rewrite.txt")flush()close()
1赞 XtremeBaumer 4/29/2022
我有一些奇怪的经历。我建议,您使用并拆分每一行来获取字段Scanner#useDelimiterScanner#nextLine,
0赞 Baek Ga Eun 4/29/2022
哦,“数字”是错别字,应该是“年龄”对不起。
0赞 user207421 4/29/2022
这远非唯一的一个。你为什么要比较?这没有意义。请复制并粘贴真实代码。示例输出显示您已更改 Milo 的国家/地区,但此代码永远无法执行此操作。agenameString
0赞 Baek Ga Eun 4/29/2022
我现在已经编辑了我帖子中的代码。很抱歉,在我发布问题后,我进行了一些更改。

答:

0赞 XtremeBaumer 4/29/2022 #1

阅读整行,用逗号拆分。然后将值分配给变量并写入新文件。

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

没有任何空行。

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

在 DOS/Windows 计算机上创建的文本文件与在 Unix/Linux 上创建的文件具有不同的行尾。DOS 使用回车符和换行符 (“\r\n”) 作为行尾,而 Unix 只使用换行符 (“\n”)

当更新分隔符以也使用 时,它同样有效\r

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

这告诉扫描程序使用 或 作为分隔符,它使用 Windows 换行符,\r\n

评论

0赞 user207421 4/29/2022
嗯,是的,但它也可以用 来完成,更干净一点。Scanner
0赞 XtremeBaumer 4/29/2022
它使用扫描仪?
0赞 Baek Ga Eun 4/29/2022
谢谢你的这个@XtremeBaumer,我会研究你的代码,因为它确实有效!谢谢!
0赞 Baek Ga Eun 4/29/2022
@user207421我使用了扫描仪,但我不知道我的错误:<
0赞 XtremeBaumer 4/29/2022
new FileWriter(output, true)您追加到文件末尾,而不是重新写入。替换为truefalse