2011-12-17 2 views
61

FileOutputStream 방법으로 작업하는 경우이 방법으로 파일을 쓸 때마다 이전 데이터가 손실됩니다. FileOutputStream을 통해 이전 데이터를 손실하지 않고 파일을 쓸 수 있습니까?이전 데이터를 잃지 않고 FileOutputStream으로 데이터를 쓰는 방법?

+1

를 사용한다고 말할

FileOutputStream(File file, boolean append) Creates a file output stream to write to the file represented by the specified File object. 
그래서

파일에 추가 할 : –

+0

[의 OutputStreamWriter 대신 FileOutputStream에 사용된다 [1] [1] : HTTP//stackoverflow.com/questions/23320070/appending-a-string-to-an-existing-file-using-outputstreamwriter/23320195?noredirect=1#comment35707473_23320195 – sourabh

+1

@PeterLawrey 스스로 배울 수있는 하나의 대개 간단하게 인터넷에 문의하십시오. 그리고 SO는 java 문서보다 첫 번째 결과입니다 :-) –

답변

114

사용하십시오 Fileboolean

FileOutputStream(File file, boolean append) 

소요 true에 부울을 설정 생성자. 이렇게하면 이미 작성된 데이터를 덮어 쓰지 않고 파일 끝에 추가하는 데이터가 추가됩니다. 당신은 당신이 일한 수있는 방법을 생각해 보는 경우에 "abc.txt"

FileOutputStream fos=new FileOutputStream(new File("abc.txt"),true); 
18

파일에 자료를 추가하기위한 생성자를 사용하여 Javadoc을 읽었을 수도 있습니다. http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html
관련 문제