Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert line break when writing to file?

Tags:

java

file-io

So My code looks like this:

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
    }
}

I get right form of output at line System.out.println(m.group(1)); However when I later on want to write what is shown by m.group(1) It writes to file without putting linebreak since the code doesn't have one.

like image 954
anno Avatar asked Nov 27 '22 11:11

anno


1 Answers

Just call fw.write(System.getProperty("line.separator"));.

System.getProperty("line.separator") will give you the line separator for your platform (whether Windows or some Unix flavor).

like image 75
mthmulders Avatar answered Dec 15 '22 02:12

mthmulders