2009-04-29 10 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

は、任意の数のバイトを削除するために、raf.setLength(raf.length() - <削除するバイト数>)を置き換えます。 – arams

+0

メタテキス​​トを追加する必要があります – keyser