0

I'm writing a program that (at one point) makes a command-line call to another native application, gets the output from that application, and puts it into a JTextPane as a String. The problem is, it doesn't seem to grab the newline characters the way it should. Because I'm using linux, each line ends with a ^M instead of a \n.

Is there any way to tell Java to look for those and create a newline in the string?

private void getSettings() {
    Commander cmd = new Commander();
    settings = cmd.getCommandOutput("hdhomerun_config " + ipAddress + " get /sys/boot");
    settingsTextPane.setText(settings);
}

I end up with the output barfed into one line and wrapped around in the text pane.

4

2 回答 2

1

我记得 Unix 显示^M回车符\r,因此您可以尝试使用String 类的replace方法替换它

settingsTextPane.setText(settings.replace('\r', '\n'));
于 2013-09-07T22:57:36.593 回答
0

谢谢大家,我再次查看了我的代码,并意识到我正在一次一行地读取程序的输出,并且只是附加这些行。我需要在我阅读的每一行的末尾添加一个 \n 。如果我错了,请纠正我,但我相信 Java 会根据您的操作系统自动更正换行符。

于 2013-09-09T22:35:09.573 回答