如何在Windows上强制Java的标准输出使用”\n”作为换行分隔符?

huangapple go评论142阅读模式
英文:

How can I force Java stdout on windows to use the "\n" line seperator?

问题

我正在为git fast-import编写一个前端。我必须使用Java(更具体地说是Java 7),因为我正在从中提取的系统实际上只有一个Java API。我认为一切都按照我认为的方式工作,除了当我将标准输出重定向到fast-import时出现以下错误:

fatal: 分支名称不符合GIT标准: refs/heads/master

当我查看行尾时,我发现有一个cr lf,而应该是lf。我尝试使用StringBuilder构建输出字符串并插入\n

StringBuilder sb = new Stringbuilder("commit refs/heads/master\n");

那没有起作用....

所以我认为是StringBuilder的问题,我尝试仅使用print()将输出写入stdout,并将指定的行尾保留为\n

System.out.print("commit refs/heads/master\n");

似乎也没有奏效....

在最后的努力中,我尝试了:

System.setProperty("line.separator","\n");
System.out.println("commit refs/heads/master");

那也失败了....

那么有没有办法在Windows控制台中用lf替换标准的cr lf

更新:

System.out.print("some text\n") 仍然产生了cr lf

供参考,以下是代码片段:

if(!isdevPath)
    System.out.print("commit refs/heads/master\n");
else
    System.out.print("commit refs/heads/devpath/" + devPathName + "\n");		
System.out.print("mark " + rev.getRevision() + "\n");
System.out.print("committer " + rev.getAuthor() + " <> " + convertDate(rev.date) + "\n");
System.out.print(exportData(rev.getDescription()) + " \n" );

我在PowerShell中运行了这个代码:Java -jar >> text.txt,当我将其重定向到git fast-import时,仍然出现相同的错误。

这是Notepad++的快照,您可以在其中看到行尾:

如何在Windows上强制Java的标准输出使用”\n”作为换行分隔符?

解决方案
答案很简单,你不能在基于Windows的shell中这样做。如果我在Windows系统中通过bash运行这个命令,它会保留来自print()语句的行尾。所以答案是在需要行尾的地方使用print()语句并使用\n,然后通过bash运行jar并将其重定向到git fast-import。

英文:

I am writing a front-end for git fast-import. I have to use java (more specifically Java 7) as the system I am extracting from really only has a java api. I have everything working the way I think it should except when I pipe my stdout over to fast-import I am getting this error:

<!-- language: none -->

fatal: Branch name doesn&#39;t conform to GIT standards: refs/heads/master 

When I look at the line endings I see that there is a cr lf versus the lf that should be there. I tried using a StringBuilder to build an output string inserting \n:

StringBuilder sb = new Stringbuilder(&quot;commit ref/heads/master\n&quot;);

that didnt work....

So thinking it was something with StringBuilder I tried just writing to stdout using print() leaving the sepcified line endings as \n.

System.out.print(&quot;commit refs/heads/master\n&quot;);

That did not seem to do the trick....

as a last ditch effort I tried:

System.setProperty(&quot;line.seperator&quot;,&quot;\n&quot;);
System.out.println(&quot;commit refs/heads/master&quot;);

that also failed failed...

So is there a way to write a simple lf in place of the standard cr lf in in a windows console?

UPDATE:

System.out.print(&quot;some text\n&quot;) still yields a cr lf.

for more reference here is the code snippet:

if(!isdevPath)
	System.out.print(&quot;commit refs/heads/master\n&quot;);
else
	System.out.print(&quot;commit refs/heads/devpath/&quot; + devPathName + &quot;\n&quot;);		
System.out.print(&quot;mark &quot; + rev.getRevision() + &quot;\n&quot;);
System.out.print(&quot;committer &quot; + rev.getAuthor() + &quot; &lt;&gt; &quot; + convertDate(rev.date) + &quot;\n&quot;);
System.out.print(exportData(rev.getDescription()) + &quot; \n&quot; );

I ran this in powershell like this: Java -jar &gt;&gt; text.txt and I am still getting the same error when I pipe to git fast-import

and here is a snap shot from notepad++ where you can see line endings

如何在Windows上强制Java的标准输出使用”\n”作为换行分隔符?

Solution
The answer to this is simply you can't do it in a windows based shell. If I run this through bash in a windows system it retains the line feeds from a print() statement. So the answer is use print() statements with a \n where you want your line feeds. Then run the jar and pipe to git fast-import from bash.

答案1

得分: 1

不要打印行:

System.out.print("一些文本\n"); // 注意是print,不是println
英文:

Don't print lines:

System.out.print(&quot;some text\n&quot;); // note print, not println

答案2

得分: 0

PrintStream 使用 System.lineSeparator()。任何你添加的代码,用于设置新的值给 line.separator (注意正确的拼写) System.setProperty("line.separator", "\n"); 不会改变 System.lineSeparator()

你可以创建一个新的 PrintStream,但要覆盖许多地方才能确保所有的 println() 调用都生效。相反,更容易的方法是将你的代码切换到使用 PrintWriter,它只需要一个覆盖的 println 方法来确保你始终获得你想要的正确结尾:

static PrintWriter newPrintWriter(Writer out, String lineSep)
{
    return new PrintWriter(out) {
        public void println() {
            print(lineSep);
        }
    };
}

这段代码将正确地输出:

PrintWriter out = newPrintWriter(new OutputStreamWriter(System.out), "\n");
out.println("commit refs/heads/master");
out.println("mark XYZ");
out.println("committer ABC");
out.flush();

你可以使用以下方式进行测试,以展示行尾的更改:

try(PrintWriter out = newPrintWriter(new FileWriter("unix.txt"), "\n"))
{
    out.println("commit refs/heads/master");
    out.println("mark XYZ");
    out.println("committer ABC");
}
try(PrintWriter out = newPrintWriter(new FileWriter("pc.txt"), "\r\n"))
{
    out.println("commit refs/heads/master");
    out.println("mark XYZ");
    out.println("committer ABC");
}
英文:

PrintStream uses System.lineSeparator(). Any code you add which sets a new value for line.separator (note correct spelling) System.setProperty(&quot;line.separator&quot;,&quot;\n&quot;); will not change System.lineSeparator().

You can make a new PrintStream but there are many places to override to make it work for all println() calls. Instead it is easier to switch your code to use PrintWriter which requires one override println method to ensure you always get right endings you wish:

static PrintWriter newPrintWriter(Writer out, String lineSep)
{
    return new PrintWriter(out) {
        public void println() {
            print(lineSep);
        }
    };
}

This code will write out correctly:

    PrintWriter out = newPrintWriter(new OutputStreamWriter(System.out), &quot;\n&quot;);
    out.println(&quot;commit refs/heads/master&quot;);
    out.println(&quot;mark XYZ&quot;);
    out.println(&quot;committer ABC&quot;);
    out.flush();

You can test with this to show the line ending changes:

    try(PrintWriter out = newPrintWriter(new FileWriter(&quot;unix.txt&quot;), &quot;\n&quot;))
    {
        out.println(&quot;commit refs/heads/master&quot;);
        out.println(&quot;mark XYZ&quot;);
        out.println(&quot;committer ABC&quot;);
    }
    try(PrintWriter out = newPrintWriter(new FileWriter(&quot;pc.txt&quot;), &quot;\r\n&quot;))
    {
        out.println(&quot;commit refs/heads/master&quot;);
        out.println(&quot;mark XYZ&quot;);
        out.println(&quot;committer ABC&quot;);
    }

huangapple
  • 本文由 发表于 2020年7月29日 08:50:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/63144771.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定