1

I try to rewrite file with changes. For this task I use next code:

FileReader reader = new FileReader(file);
BufferedReader br = new BufferedReader(reader);
String line;
StringBuilder builder = new StringBuilder();
while((line = br.readLine()) != null) {
    builder.append(line);
}
builder.delete(0, 3);
FileWriter writer = new FileWriter(file);
writer.write(builder.toString());
writer.flush();
writer.close();

But my code removes all CR (end of line) symbols and I get

bbb ccc

instead of

bbb ccc How can I say to Java to skip removing CR symbols?

Alex
  • 1,781
  • 4
  • 25
  • 41
  • this `br.readLine()` reads a line, without CR of LF. hence, when you concat the results from successive calls to `br.readLine()`, you loose those signs. It's a file. You can read it all at once, see http://stackoverflow.com/questions/14169661/read-complete-file-without-using-loop-in-java (since anyway you are putting the whole file in memory) – njzk2 Feb 11 '15 at 16:41

4 Answers4

2

By not using br.readline(), which seems to read a line and remove the trailing line break, or by inserting it back in before adding it to the string builder.

EDIT: here's BufferedReader.readLine()'s documentation, which clearly states it removes your trailing line break.

Marcus Müller
  • 27,924
  • 4
  • 40
  • 79
0

When you append your new line, append \n as well:

while((line = br.readLine()) != null) {
    builder.append(line);
    builder.append("\n");
}

Platform-independent newline would be

builder.append(System.getProperty("line.separator"));

See also:

Community
  • 1
  • 1
Miljen Mikic
  • 13,521
  • 5
  • 51
  • 59
0

I suggest this solution

    byte[] bytes = Files.readAllBytes(Paths.get(file));
    try (FileOutputStream out = new FileOutputStream(file)) {
        out.write(bytes, 3, bytes.length - 3);
    }
Evgeniy Dorofeev
  • 124,221
  • 27
  • 187
  • 258
0

As mentioned in other answers, you shouldn't use readLine for this filecopy operations to preserve linefeeds and carriage-returns.
But except for the working with small files I would also not recommend to hold the whole file contents inside a buffer (byte[], StringBuilder,...).

Here is an simple example to show how to copy a file bytewise with skipping some leading bytes (like in you example). Depending on the further manipulation you want to do, you can enlarge the bytebuffer to fit your needs, but than you should check the count of read bytes.

public class FileCopyTest {

    public static void main(String[] args) throws IOException {
        copyFile(3,"c:/file.txt","c:/fileCopy.txt");
    }


    public static void copyFile(int skipFirst, String src, String dst) throws IOException {
        int toSkip = skipFirst;
        try (FileInputStream fileIn = new FileInputStream(src); 
             FileOutputStream fileOut = new FileOutputStream(dst)) {
            byte read[] = new byte[1];
            while (-1 < fileIn.read(read, 0, 1)) {
                if(toSkip > 0 ) {
                    toSkip--;
                    continue;
                }
                fileOut.write(read,0,1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
Gren
  • 1,784
  • 1
  • 8
  • 15