设置以Java结尾的默认行 [英] Set default line ending in Java

查看:129
本文介绍了设置以Java结尾的默认行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在多平台环境(Windows + Linux)中使用Java。但是,无论我们生成什么文本文件,都应使用LF作为其EOL序列。

I'm working with Java in a multi-platform environment (Windows + Linux). Whatever text files we produce, however, should use LF as their EOL sequence.

我是否可以强制Java使用此EOL序列,无论我在运行程序的平台上如何?我不想改变整个程序,即所有对System.out.println和类似程序的调用都应保持原样,只有函数末尾的* ln应该总是输出0x0A并且永远不会 0x0D 0x0A。

Is there a way I can force Java to use this EOL sequence regardless on the platform I'm running a program on? I'd like not to have to change the whole program, i.e. all calls to System.out.println and similar should be kept as is, only the *ln at the end of the function should always output an "0x0A" and never "0x0D 0x0A".

我可以想到两种不同的方式,但我不知道是否有可能:

I could think of two different ways of doing that, but I don't know if either one is possible:


  1. 覆盖平台相关的默认EOL序列

  2. 使Java相信即使我在DOS命令行上运行程序,我也在运行Linux

这有可能吗?或者其他什么呢?

Is any of this possible? Or something else perhaps?

推荐答案

您可以尝试设置相应的系统属性,如 System.setProperty( line.separator,你需要的东西); (它也可以通过命令行参数实现到JVM)

You can try setting the corresponding system property like System.setProperty("line.separator", "something you need"); (It can also be achieved throug command-line parameters to JVM)

或者,也许,您可以使用打印 insetad println 并打印换行符(\\ \\ r,\ n或他们的组合)你需要的地方。

Or, maybe, you can use print insetad of println and print line-breaking characters ("\r", "\n" or their combination) where you need it.

这篇关于设置以Java结尾的默认行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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