java system newline_使用独立于系统的换行符 System Independent Newline Characters

Categories: Java; Tagged with: Java; @ October 14th, 2010 22:42

这几天在写一段处理’代码’的代码, 过程简单: 读取.java源文件的代码, 处理, 写入目标文件.

为了测试简单, 直接使用’\n’作为换行符, Eclipse控制台打印出来无误. 随后写入文件, Windows记事本打开, 空行变多, 不解, 于是拖入Eclipse查看, 显示无误. 但记事本会无端增加空行若干. 极其不解.

Unix. The ‘\n’ character represents the single Unicode character with the value 10 (‘\u000A’) and is used to separate lines in Unix files. This character is also sometimes called linefeed.

Windows. Windows programs often process a ‘\n’ separated text file correctly (NotePad is a exception), but many expect a pair of characters (carriage return followed by line feed = "\r\n") Use the method below to get the newline string that is appropriate for your system.

Mac. In the past, the Apple Mac requirse lines to be separated by ‘\r’, but their move toward Unix (System X) probably means they also accept ‘\n’. I haven’t used a Mac in quite a while tho, so I’m not positive.

我处理的方式是按照Unix换行符’\n’来处理的, 因此在Windows上不管是输入还是输出都会有问题.

System independent value. You can get the value for the system your Java program is running on from the system properties. It is essential to do this with portable programs, and you should always assume your program is portable, eg, that it might run as an applet or using Webstart.

为了保证可移植性, 务必使用: System.getProperty("line.separator")来取得当前系统的换行符.

public static String newline = System.getProperty("line.separator");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值