有没有办法使PrintWriter输出为UNIX格式? [英] Is there a way to make PrintWriter output to UNIX format?

查看:104
本文介绍了有没有办法使PrintWriter输出为UNIX格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当然是在Java中。我正在编写程序并在Windows环境下运行它,但我需要输出(.csv)以Unix格式完成。任何简单的方案?谢谢!

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!

推荐答案

注意:正如评论中所报告的那样,下面给出的方法在JDK 9+中打破了。在James H.的回答中使用这种方法。

Unix格式是指使用\ n作为行终止符而不是\\\\ n?只需在创建PrintWriter之前设置 line.separator 系统属性。

By "Unix format" do you mean using "\n" as the line terminator instead of "\r\n"? Just set the line.separator system property before you create the PrintWriter.

就像演示一样:

import java.io.*;

public class Test
{
    public static void main(String[] args)
        throws Exception // Just for simplicity
    {
        System.setProperty("line.separator", "xxx");
        PrintWriter pw = new PrintWriter(System.out);
        pw.println("foo");
        pw.println("bar");
        pw.flush();
    }
}

当然,它为整个JVM设置了它,并不理想,但可能就是你所需要的一切。

Of course that sets it for the whole JVM, which isn't ideal, but it may be all you happen to need.

这篇关于有没有办法使PrintWriter输出为UNIX格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆