Is there a way to make PrintWriter output to UNIX format?

前端 未结 5 428
梦谈多话
梦谈多话 2020-12-11 16:46

In Java, of course. I\'m writing a program and running it under a Windows environment, but I need the output (.csv) to be done in Unix format. Any easy solution? Thanks!

5条回答
  •  -上瘾入骨i
    2020-12-11 17:01

    Assuming the formatting issue you refer to is that Windows line breaks are Carriage Return-Line Feed ("\r\n") while Unix ones are Line Feed ("\n") only, the easiest way to make sure your file uses LF and not CRLF is to eschew println and instead use print("\n") to terminate lines.

    So instead of:

    writer.println("foo,bar,88");
    

    use

    writer.print("foo,bar,88\n");
    

    You can just search the relevant files for println to make sure you catch them all.

提交回复
热议问题