0

I build a string on a unix server and write it then down on a windows maschine. To make a linefeed I use "\r\n" but then the server only adds the unix linefeed "\n". The point is the eol is in hex 0a and I need 0d0a for other programs.

Has anyone an idea to convert the linefeeds before writing it down on the windows maschine?

To convert the string to hex then replace all 0a with 0d0a an convert it back to a string is not the best practise. Has anyone a better solution?

4
  • What type is br? I'd think BufferedReader but obviously that's input, not output... Commented May 17, 2016 at 7:44
  • Your question briefly claimed that br was a BufferedWriter. BufferedWriter has no appendLine method. So, again, if you tell us what type the br in your br.appendLine() is, maybe we can help you more. Commented May 17, 2016 at 8:04
  • Sorry, I edited the Question, appendLine() is an own written method that simlpy added a "\r\n". Commented May 17, 2016 at 8:12
  • Well, writing \r\n to a file absolutely writes \r\n to the file, not just \n, even on *nix. You'll need to provide more information. Commented May 17, 2016 at 8:15

2 Answers 2

4

Writing "\r\n" to a file will output "\r\n", not "\n", to the file, even on *nix. Here's an example, using a BufferedWriter since at one point you said you were using one:

import java.io.*;

public class Example {
    public static final void main(String[] args) {
        System.out.println("Writing to test.txt");
        try (
            Writer w = new FileWriter("test.txt");
            BufferedWriter bw = new BufferedWriter(w);
        ) {
            bw.append("Testing 1 2 3");
            bw.append("\r\n");
            bw.append("More Testing");
            bw.append("\r\n");
        }
        catch (IOException ioe) {
            System.err.println("Error writing to file: " + ioe.getMessage());
        }
        System.out.println("Done");
    }
}

Running it:

$ java Example
Writing to test.txt
Done

Proof it writes \r\n (not just \n) to the output (yes, I'm using *nix, specifically Linux Mint 17.3):

$ hexdump -C test.txt
00000000  54 65 73 74 69 6e 67 20  31 20 32 20 33 0d 0a 4d  |Testing 1 2 3..M|
00000010  6f 72 65 20 54 65 73 74  69 6e 67 0d 0a           |ore Testing..|
0000001d
Sign up to request clarification or add additional context in comments.

4 Comments

I tried this already but it seems like unix cannot resolve the \r.
@JannickHerrmann: Yes, it can. Maybe if you tell us what br is, a bit more context, ...
@JannickHerrmann: Your updated question is more general, just asking how to do it. This answer still answers it. I've added an example.
Thanks a lot! I found the mistake. A incorrect overwritten method was the problem.
0

If you really want to change default for entire application, redefine line.separator system property at startup (with -D parameter). But it is quite heavyweight solution, it might be easier to just explicitly write newline characters for particular reports you need, while leaving all the rest (like native log files or whatever) with unix newlins.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.