在java中保留换行符和回车符(groovy)

abi*_*964 0 java groovy

我正在尝试从 Linux 盒子获取命令输出

例如:tnsping

我想保留它的换行符。

下面是我添加命令并将frows其传递给函数执行的代码

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]){}
}
}
Run Code Online (Sandbox Code Playgroud)

在下面的代码中,我正在读取结果,根据换行符分割结果,这样我的所有换行符都消失了

    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
  }
Run Code Online (Sandbox Code Playgroud)

更新 我如何保留换行符或回车符,以便当使用 xsl 打开 xml 时以及遇到<pre></pre>它时应该保留其格式?

MBy*_*ByD 6

如果我猜对了,你应该使用\rand \n:

result.append(line+"\r\n");
Run Code Online (Sandbox Code Playgroud)

或者,您可以从系统获取行分隔符:

result.append(line+System.getProperty("line.separator"));
Run Code Online (Sandbox Code Playgroud)