Java更改系统换行符 [英] Java change system new-line character

查看:80
本文介绍了Java更改系统换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows上,使用System.out.println()将打印出\n\r,而在Unix系统上,您将得到\n.

On Windows, using System.out.println() prints out \n\r while on a Unix system you would get \n.

有什么办法告诉java您想使用哪些换行符?

Is there any way to tell java what new-line characters you want to use?

推荐答案

是的,有一种方法,我已经尝试过了.

Yes, there is a way and I've just tried it.

有一个系统属性line.separator.您可以使用System.setProperty("line.separator", whatever)

There is a system property line.separator. You can set it using System.setProperty("line.separator", whatever)

为确保确实导致JVM使用其他分隔符,我实施了以下练习:

To be sure that it indeed causes JVM to use other separator I implemented the following exercise:

    PrintWriter writer = new PrintWriter(new FileWriter("c:/temp/mytest.txt"));
    writer.println("hello");
    writer.println("world");
    writer.close();

我现在正在Windows上运行,所以文件的结果是14个字节长

I am running on windows now, so the result was 14 bytes long file:

03/27/2014  10:13 AM                14 mytest.txt
               1 File(s)             14 bytes
               0 Dir(s)  409,157,980,160 bytes free

但是,当我在代码的开头添加以下行时:

However when I added the following line to the beginning of my code:

    System.setProperty("line.separator", "\n");

我有14个字节长的文件:

I got 14 bytes long file:

2014年3月27日10:13 14 mytest.txt 1个文件14个字节 0 Dir(s)409,157,980,160字节免费

03/27/2014 10:13 AM 14 mytest.txt 1 File(s) 14 bytes 0 Dir(s) 409,157,980,160 bytes free

我使用记事本打开了该文件,该记事本无法将单个\n识别为新行,并看到了一行文本helloworld而不是2条单独的行.因此,这有效.

I opened this file with notepad that does not recognize single \n as a new line and saw one-line text helloworld instead of 2 separate lines. So, this works.

这篇关于Java更改系统换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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