RandomeAccessFile
use write replace writeBytes
public class RandomAccessFileTest { public static void main(String[] args) { try { RandomAccessFile file= new RandomAccessFile("c://temp//in.txt","rw"); System.out.println(file.readLine()); file.seek(file.length()); //clear all// file.setLength(0l); //use write() to replace writeBytes file.write("hello ross test".getBytes()); //writeBytes will has issue on UTF8// file.writeBytes(""); file.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}