在java中保持换行符和回车符(groovy) [英] keeping the newline and carriage return characters in java (groovy)
问题描述
我试图从Linux中获取命令输出
I am trying to get command output from Linux box
例如: tnsping
,我想保留它的换行符。
and I want to preserve it's newline characters.
以下代码将命令添加到 frows
并将其传递给执行的函数
Below is the code where I add command(s) to frows
and pass it to a function for execution
def oracleCommand(csm,pluginOutputs)
{
HashMap<String,String> params = IntermediateResults.get("userparams")
Map env=AppContext.get(AppCtxProperties.environmentVariables)
def fClass = new GroovyClassLoader().parseClass( new File( 'plugins/infa9/Infa9CommandExecUtil.groovy' ) )
List<String> frows
frows=["tnsping $params.tns"] //for the time being only one command is here
List<String> resultRows = fClass.newInstance().fetchCommandOutput( params, env, frows )
csm.oracleCommand(){
oracle_input(frows[0]){}
oracle_output(resultRows[0]){}
}
}
我正在阅读结果,根据换行符拆分结果,以便我所有的换行符都不再使用。
In the below code I am reading the result, splitting result based on newlines so all my newlines are gone
public List<String> fetchCommandOutput( Map<String,String> params, Map env, List<String> rows )
{
List<String> outputRows = new ArrayList<String>()
try
{
for(item in rows)
{
String temp=item.toString()
Process proc = Runtime.getRuntime().exec(temp);
InputStream stdin = proc.getInputStream();
InputStreamReader isr = new InputStreamReader(stdin);
BufferedReader br = new BufferedReader(isr);
String line = null;
result = new StringBuffer()
line=null
while ((line = br.readLine()) != null)
{
result.append(line+" #10#13 ") //Here i am trying to add the newline again, but it does not reflect in the generated xml. it just come as plain text
}
String tRes=result
tRes=tRes.trim()
outputRows.add(tRes)
int exitVal = proc.waitFor();
}
}
catch (IOException io)
{
io.printStackTrace();
}
catch (InterruptedException ie)
{
ie.printStackTrace();
}
return outputRows
}
update
如何保留我的换行符或回车符,以便当用xsl打开xml并遇到< pre>< / pre>
它应该保留它的格式?
update
How can i preserve my newlines or carriage returns, such that when the xml is opened with a xsl and when encountered <pre></pre>
it should preserve its formatting?
推荐答案
c> \r 和 \\\
:
result.append(line+"\r\n");
或者,您可以从系统中获取行分隔符:
or, you can get the line separator from the system:
result.append(line+System.getProperty("line.separator"));
这篇关于在java中保持换行符和回车符(groovy)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!