Code example for RandomAccessFile

Methods: getChannel, setLength

0
    private boolean mShutdownHookExecuted = false;
 
    public ContextFileWriter(Logger logger, File file) throws IOException {
        mLogger = logger;
        mLogger.info("Opening for writing: " + file.getAbsolutePath());
        RandomAccessFile raFile = new RandomAccessFile(file, "rw");
        raFile.setLength(0);
        mChannel = raFile.getChannel();
        writeHeader(); 
 
        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() { shutdownHook(); } 
        }); 
    } 
 
    private synchronized void shutdownHook() { 
        try { 
            if (mChannel.isOpen()) {
                writeBuffer(); 
            } 
        } catch (IOException e) {