英文:
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的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'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("commit ref/heads/master\n");
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("commit refs/heads/master\n");
That did not seem to do the trick....
as a last ditch effort I tried:
System.setProperty("line.seperator","\n");
System.out.println("commit refs/heads/master");
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("some text\n")
still yields a cr
lf
.
for more reference here is the code snippet:
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" );
I ran this in powershell like this: Java -jar >> 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
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("some text\n"); // 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("line.separator","\n");
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), "\n");
out.println("commit refs/heads/master");
out.println("mark XYZ");
out.println("committer ABC");
out.flush();
You can test with this to show the line ending changes:
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");
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论