如何在Java应用程序中强制使用Windows行结尾? [英] How do I force Windows line endings in Java app?

查看:149
本文介绍了如何在Java应用程序中强制使用Windows行结尾?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java应用程序写入文件:

  BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile) ); 
bw.write(line + lineTermination);

行终止定义为:

  \\\ n 

我变得奇怪,神秘空白行插入到我的文件中。



如果我将代码更改为:



<$ p $我没有额外的行p> bw.write(线);
bw.newLine();

但是我想强制特定的行结束,而不是使用System属性。客户特别要求一个行结束字符 - 有些甚至有|。它不是一个可行的解决方案,只需使用\ n。



以下是缺少行的数据片段:

  KABE, 14/01/11, 14时35分, 14点56分, 1987, 美国, SS,CO ,与孩子结婚,,EINE SCHRECKLICH NETTE FAMILIE,,N,10,,12,O'NEILL ED,13,SAGAL KATEY 

PRO7,14/01/11,14:35,14:55,2001,US,SS,CO, SCRUBS,,SCRUBS DIE ANFAENGER,,C,10,BERNSTEIN ADAM,12,BRAFF ZACH,13,CHALKE SARAH

感谢您的时间: - )

解决方案

你可以打电话

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

以便在代码中设置系统属性。


I have a Java app that writes to a file with:

BufferedWriter bw = new BufferedWriter(new FileWriter(outputFile));
bw.write(line + lineTermination);

Line termination is defined as:

\r\n

I get the odd, mysterious blank line inserted into my file.

I get no extra lines if I change my code to:

bw.write(line);
bw.newLine();

But I want to force a specific line ending, not use System property. Clients specifically request a line ending character - some even have |. Its not a viable fix to just use \n.

Here is an snippet of the data with missing line:

"KABE","14/01/11","14:35","14:56","1987","US","SS","CO","MARRIED WITH CHILDREN","","EINE SCHRECKLICH NETTE FAMILIE","","N","10","","12","O'NEILL ED","13","SAGAL KATEY"

"PRO7","14/01/11","14:35","14:55","2001","US","SS","CO","SCRUBS","","SCRUBS DIE ANFAENGER","","C","10","BERNSTEIN ADAM","12","BRAFF ZACH","13","CHALKE SARAH"

Thanks for your time :-)

解决方案

You can call

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

in order to set the system property inside your code.

这篇关于如何在Java应用程序中强制使用Windows行结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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