Saving newline and return characters in java (groovy)

I am trying to get command output from a Linux window

for example: tnsping

and I want to keep newline characters.

Below is the code where I add the commands (s) to frows and pass them functions to execute

 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 code below, I am reading the result, the result of splitting based on new lines, so all my translation characters are not displayed

  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 How can I save my newlines or carriage returns so that when xml is opened using xsl and when <pre></pre> is encountered, should it retain its formatting?

0
source share
2 answers

If I get you right, you should use \r and \n :

 result.append(line+"\r\n"); 

or you can get the line separator from the system:

 result.append(line+System.getProperty("line.separator")); 
+2
source

If you make your function more Groovy:

  public List<String> fetchCommandOutput( Map<String,String> params, Map env, List<String> rows ) { rows.collect { item -> def (outtxt, errtxt) = [new StringBuffer(), new StringBuffer()].with { inbuf, errbuf -> def proc = item.execute() proc.waitForProcessOutput( inbuf, errbuf ) [ inbuf, errbuf ]*.toString()*.trim() } if( errtxt ) { println "Got error $errtxt running $item" } outtxt } } 

I believe that it supports output in exactly the same format as the outputs of the process ... And you have less code to support ...

+1
source

Source: https://habr.com/ru/post/1380166/


All Articles