Java 8 RandomAccessFile - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 RandomAccessFile class.

This code example shows how to use the following methods:close, getFD, write, sleep
    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. 
   *  

9
CodeRank
This code example shows how to use the following methods:length, read, skipBytes
        } 
 
        //our yaml parser seems to be too tolerant. Each yaml suite must end with \n, otherwise clients tests might break. 
        RandomAccessFile randomAccessFile = null;
        try { 
            randomAccessFile = new RandomAccessFile(file, "r");
            randomAccessFile.skipBytes((int)randomAccessFile.length() - 1);
            if (randomAccessFile.read() != 10) {
                throw new RestTestParseException("test suite [" + api + "/" + filename + "] doesn't end with line feed (\\n)");
            } 
        } finally { 
            IOUtils.close(randomAccessFile);
        } 
 
        XContentParser parser = YamlXContent.yamlXContent.createParser(new FileInputStream(file));
        try { 
            RestTestSuiteParseContext testParseContext = new RestTestSuiteParseContext(api, filename, parser);
            return parse(testParseContext);
        } catch(Exception e) {
            throw new RestTestParseException("Error parsing " + api + "/" + filename, e);
9
CodeRank
This code example shows how to use the following methods:getChannel
 
    private final AtomicInteger refCount = new AtomicInteger();
 
    public RafReference(File file) throws FileNotFoundException {
        this.file = file;
        this.raf = new RandomAccessFile(file, "rw");
        this.channel = raf.getChannel();
        this.refCount.incrementAndGet();
    } 
 
    public File file() {
        return this.file;
    } 
 
    public FileChannel channel() {
        return this.channel;
    } 
 
    public RandomAccessFile raf() {
        return this.raf;
8
CodeRank
This code example shows how to use the following methods:close, getFD
        options.inDither = false;
        options.inSampleSize = sampleSize;
 
        RandomAccessFile file = null;
        try { 
            file = new RandomAccessFile(imagePath, "r");
            return BitmapFactory.decodeFileDescriptor(file.getFD(), null,
                    options);
        } catch (IOException e) {
            Log.d(TAG, e.getMessage(), e);
            return null; 
        } finally { 
            if (file != null)
                try { 
                    file.close();
                } catch (IOException e) {
                    Log.d(TAG, e.getMessage(), e);
                } 
        } 
    } 
8
CodeRank
This code example shows how to use the following methods:close, seek, write
  private void checkFileCorruption(LocalFileSystem fileSys, Path file, 
                                   Path fileToCorrupt) throws IOException {
     
    // corrupt the file  
    RandomAccessFile out = 
      new RandomAccessFile(new File(fileToCorrupt.toString()), "rw");
     
    byte[] buf = new byte[(int)fileSys.getFileStatus(file).getLen()];    
    int corruptFileLen = (int)fileSys.getFileStatus(fileToCorrupt).getLen();
    assertTrue(buf.length >= corruptFileLen);
     
    rand.nextBytes(buf);
    out.seek(corruptFileLen/2);
    out.write(buf, 0, corruptFileLen/4);
    out.close();
 
    boolean gotException = false;
     
    InputStream in = fileSys.open(file);
    try { 
8
CodeRank
This code example shows how to use the following methods:close, getFD
        options.inDither = false;
        options.inSampleSize = sampleSize;
 
        RandomAccessFile file = null;
        try { 
            file = new RandomAccessFile(imagePath, "r");
            return BitmapFactory.decodeFileDescriptor(file.getFD(), null,
                    options);
        } catch (IOException e) {
            Ln.d(e, "Could not get cached bitmap.");
            return null; 
        } finally { 
            if (file != null)
                try { 
                    file.close();
                } catch (IOException e) {
                    Ln.d(e, "Could not get cached bitmap.");
                } 
        } 
    } 
8
CodeRank
This code example shows how to use the following methods:close, length, seek, writeInt
        Context applicationContext = getApplicationContext();
        File resultFile = new File(applicationContext.getFilesDir(), getId());
        try { 
            // Append a constant value in result file, if services crashed and is relaunched, size 
            // of the result file will be too big. 
            RandomAccessFile raf = new RandomAccessFile(resultFile, "rw");
            raf.seek(raf.length());
            Log.i(TAG, "Writing 0x42434445 at " + raf.length() + " in " + resultFile.getPath());
            raf.writeInt(0x42434445);
            raf.close();
        } catch (IOException e) {
            e.printStackTrace();
        } 
        MultiDex.install(applicationContext);
        Log.i(TAG, "Multi dex installation done.");
 
        int value = getValue();
        Log.i(TAG, "Saving the result (" + value + ") to " + resultFile.getPath());
        try { 
            // Append the check value in result file, keeping the constant values already written. 
8
CodeRank
This code example shows how to use the following methods:getChannel, length, seek, setLength, write
    public static void main(String[] args) throws IOException {
        ///File f = File.createTempFile("aaa","aaa"); 
        File f = new File("test");
        f.deleteOnExit();
 
        RandomAccessFile raf = new RandomAccessFile(f, "rw");
 
        raf.setLength((long) 1e6);
 
        System.out.println("length is " + raf.length());
 
 
        raf.seek((long) 2e6);
        raf.write(1);
 
        System.out.println("length is " + raf.length());
 
        MappedByteBuffer b = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, (long) 3e6);
 
        System.out.println("length after mapping is " + raf.length());

8
CodeRank
This code example shows how to use the following methods:close, getChannel, length
    @Override 
    public void messageReceived(ChannelHandlerContext ctx, String msg) throws Exception {
        RandomAccessFile raf = null;
        long length = -1;
        try { 
            raf = new RandomAccessFile(msg, "r");
            length = raf.length();
        } catch (Exception e) {
            ctx.writeAndFlush("ERR: " + e.getClass().getSimpleName() + ": " + e.getMessage() + '\n');
            return; 
        } finally { 
            if (length < 0 && raf != null) {
                raf.close();
            } 
        } 
 
        ctx.write("OK: " + raf.length() + '\n');
        if (ctx.pipeline().get(SslHandler.class) == null) {
            // SSL not enabled - can use zero-copy file transfer. 
            ctx.write(new DefaultFileRegion(raf.getChannel(), 0, length));

8
CodeRank
This code example shows how to use the following methods:close, readLine
 
    RandomAccessFile reader = null;
    String load = null;
 
    try { 
      reader = new RandomAccessFile("/proc/stat", "r");
      load = reader.readLine();
    } catch (IOException ex) {
      Log.e("LinuxUtils", "Failed to read /proc/stat", ex);
    } finally { 
      try { 
        reader.close();
      } catch (IOException e) {
        Log.e("LinuxUtils", "Failed to close stream");
      } 
    } 
 
    return load;
  } 
 
7
CodeRank

Related Java 8 RandomAccessFile Questions & Answers:

See Code Examples for Java 8 RandomAccessFile Methods: