2009-04-29 7 views
3

바이트를 temp.fls 파일에 쓰고 있습니다. 작업을 완료 한 후 temp.fls 파일에서 마지막 256 바이트를 삭제하려고합니다. 이것을 어떻게 할 수 있습니까? 도와주세요.Java에서 파일 내용 삭제

미리 감사드립니다. 그래서 같은

답변

10

사용 RandomAccessFile.setLength() :

RandomAccessFile f = new RandomAccessFile(yourFile,"rw"); 
f.setLength(f.length()-256); 
+0

PLZ 나에게 몇 가지 예제 코드 –

+0

@rekha을 제공 : spdenne는 당신에게 API 문서에 대한 링크를했다. 그것과 건설자를 한번보세요. – boutta

0
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.RandomAccessFile; 

public class RandomAccessFileDemo { 

    public static void main(String ... args) throws FileNotFoundException, IOException 
    { 
     File file = new File(<FILE_PATH>); 
     System.err.println("MSR:: RandomAccessFileDemo :: the target length is"+file.length()); 
     RandomAccessFile raf = new RandomAccessFile(file,"rwd"); 
     raf.seek(file.length()-1); // Set the pointer to the end of the file 
     System.err.println("MSR:: RandomAccessFileDemo :: the file pointer is"+raf.getFilePointer()); 

     raf.setLength(raf.length()-raf.length()); 

    } 
} 
+0

도움이되고 성공적으로 테스트되기를 바랍니다. – arams

+0

은 임의의 수의 바이트를 제거하기 위해 Stephen이 제안한대로 raf.setLength (raf.length() - <삭제할 바이트 수>)를 대체합니다. – arams

+0

일부 메타 텍스트를 추가해야합니다. – keyser