首先,我輸出文件的內容,這裏是我的代碼。然後,我會做一些字符串工作來編輯每一行。如果我要保存更改,怎麼辦?我可以做到這一點,而無需創建一個tmp文件?如何更新Java中的文件內容
String executeThis = "cat" + " " + "/var/lib/iscsi/nodes/"
+ iscsiInfo.selectedTargets2.get(i) + "/" + myString + "/default";
String inputThis = "";
Process process = ServerHelper.callProcessWithInput(executeThis, inputThis);
try {
logger.debug("stdOutput for editing targets credential:");
BufferedReader stdOutput = new BufferedReader(
new InputStreamReader(process.getInputStream()));
String s = null;
while ((s = stdOutput.readLine()) != null) {
logger.info("The content [email protected]@@@@@@@@@@@@@@@@@@@@@@@"+s)
// do something to edit each line and update the file
}
} catch (IOException e) {
logger.fatal(e);
}
你爲什麼要執行一個進程而不是使用'FileReader'? – McLovin
是的,你可以通過關閉正在進行文件更改的流來做到這一點! – Devavrata
使用BufferedWriter http://docs.oracle.com/javase/7/docs/api/java/io/BufferedWriter.html。我不會建議每次關閉並重新打開流。 – Kode