Code example for RandomAccessFile

Methods: close, getFD, sleep, write

0
    Random random = new Random();
    byte[] data = new byte[size];
    random.nextBytes(data);
 
    newFile.createNewFile();
    RandomAccessFile file = new RandomAccessFile(newFile, "rws");
 
    file.write(data);
       
    file.getFD().sync();
    file.close();
  } 
 
  /** 
   * Verify that du returns expected used space for a file. 
   * We assume here that if a file system crates a file of size  
   * that is a multiple of the block size in this file system, 
   * then the used size for the file will be exactly that size. 
   * This is true for most file systems. 
   *  
   * @throws IOException 
   * @throws InterruptedException 
   */ 
  public void testDU() throws IOException, InterruptedException { 
    final int writtenSize = 32*1024;   // writing 32K
    // Allow for extra 4K on-disk slack for local file systems 
    // that may store additional file metadata (eg ext attrs). 
    final int slack = 4*1024;
    File file = new File(DU_DIR, "data");
    createFile(file, writtenSize);
 
    Thread.sleep(5000); // let the metadata updater catch up
     
    DU du = new DU(file, 10000);
    du.start();
    long duSize = du.getUsed();
    du.shutdown();