Search code examples
javafilewriter

how to jump a character in FileWriter in java?


Here I'm trying to write program which reads from one file and prints to other file with little modification.

I have to write to other file in such a way that I have to print new line right next from ';' character. I tried the below code snippet but ended up printing newline from the ';' character.

Here is snippet

try (FileReader fr = new FileReader(file); FileWriter fw = new FileWriter(file2)) {
            while ((c = fr.read()) != -1) {
                if (c == 59) {
                    fw.write("\n");
                }
                fw.write((char) c);
            }
        }

Here is the file snippet I'm trying to read

.menu ul li{ float:left; padding:0px; margin:0px; font-size:13px;}
.menu ul li a{ display:block; float:left; background-color:#f3d987; padding:7px 12px 5px 12px; color:#000; text-decoration:none;
margin:0 4px 0 0;

Solution

  • You just have to write the character after the newline.

    try (FileReader fr = new FileReader(file); FileWriter fw = new FileWriter(file2)) {
        while ((c = fr.read()) != -1) {
            fw.write((char) c);
            if (c == 59) {
                fw.write("\n");
            }
        }
    }