2011-11-03 45 views
0

我想從Linux中保持Java中的換行符和回車符(常規)

如get命令的輸出:tnsping

,我想保留它的換行符。

下面是我add命令(縣)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]){} 
} 
} 

在下面的代碼中,我正在讀的結果代碼,拆分結果基於換行符所以我所有的換行符都不見了

​​

更新 我怎樣才能保持我的新行或者回車,這樣當XML開設有XSL和遇到當它應該保留它的格式?

+0

您是否考慮過讓Groovy代碼更加Groovy?這是非常多的Java ...我認爲你可以相當大地減小它的大小 –

+0

@tim_yates:是的,你是對的,但在Java中編碼的習慣總是來的方式:) – abi1964

+1

添加了一個答案,讓你更多的代碼groovy,並處理文本,你想要的 –

回答

2

如果我得到你的權利,你應該使用\r\n

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

或者,您可以從系統中獲得的行分隔符:

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

如果你讓你的功能更多的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 
    } 
    } 

我相信它保持輸出格式完全相同作爲過程輸出...並且您擁有更少的代碼來維護...

相關問題