먼저 파일 내용을 출력합니다. 여기에 제 코드가 있습니다. 그리고 나서 각 줄을 편집하기 위해 문자열 작업을 할 것입니다. 변경 사항을 저장하려면 어떻게해야합니까? 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