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

前端 未结 5 430
梦谈多话
梦谈多话 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条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-11 17:10

    A paranoid programmer would synchronize on the system properties object, at least if different Printwriters needs different types of line terminators.

    public static PrintWriter createPrintWriter(OutputStreamWriter out, boolean autoflush){
        Properties props = System.getProperties();
        synchronized (props) {
            Object old = null;
            try {
            old = props.setProperty("line.separator", "\n");
            return  new PrintWriter(out, autoflush);
            } finally {
                if( old != null ) {
                    props.put("line.separator", old);
                }
            }
        }
    }
    

提交回复
热议问题