写入文件时插入换行符?

ann*_*nno 4 java file-io

所以我的代码看起来像这样:

try {
    while ((line = br.readLine()) != null) {
        Matcher m = urlPattern.matcher (line);
        while (m.find()) {
            System.out.println(m.group(1));

            //the println puts linebreak after each find

            String filename= "page.txt";
            FileWriter fw = new FileWriter(filename,true);
            fw.write(m.group(1));
            fw.close();

            //the fw writes everything after each find with no line break
    }
}
Run Code Online (Sandbox Code Playgroud)

我在行输出正确的形式System.out.println(m.group(1));然而当我以后想要写m.group(1)它显示的内容它写入文件而不放置换行符,因为代码没有.

mth*_*ers 24

打电话吧fw.write(System.getProperty("line.separator"));.

System.getProperty("line.separator") 将为您的平台提供行分隔符(无论是Windows还是某些Unix风格).