Example usage for java.io RandomAccessFile RandomAccessFile

List of usage examples for java.io RandomAccessFile RandomAccessFile

Introduction

In this page you can find the example usage for java.io RandomAccessFile RandomAccessFile.

Prototype

public RandomAccessFile(File file, String mode) throws FileNotFoundException 

Source Link

Document

Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.

Usage

From source file:com.dc.gameserver.extComponents.Kit.ip.IPSeeker.java

private IPSeeker() {

    ipCache = new HashMap<String, IPLocation>();

    try {//from   w  ww.j a  v a  2s . co  m
        ipFile = new RandomAccessFile(IPSeeker.class.getClassLoader().getResource("").getPath() + IP_FILE, "r");
        // ???
        if (ipFile != null) {
            try {
                ipBegin = readLong4(0);
                ipEnd = readLong4(4);
                if (ipBegin == -1 || ipEnd == -1) {
                    ipFile.close();
                    ipFile = null;
                }
            } catch (IOException e) {
                log.info("IP???IP");
                ipFile = null;
            }
        }
    } catch (FileNotFoundException e) {
        log.info("IP??IP");
    }
}

From source file:com.taobao.common.tfs.impl.LocalKey.java

public void loadFile(String fileName) throws TfsException, IOException {
    localKeyName = fileName;//from  w  ww . ja  v  a 2 s .  c  om
    fileOp = new RandomAccessFile(fileName, "rw");
    load();
}

From source file:fm.moe.android.util.JSONFileHelper.java

public static void write(final JSONObject object, final String path) throws IOException {
    if (object == null || path == null)
        return;/*from w w w .  j a  v  a  2s .c  o  m*/
    new File(path).delete();
    final RandomAccessFile raf = new RandomAccessFile(path, FILE_MODE_RW);
    final FileWriter fw = new FileWriter(raf.getFD());
    fw.write(object.toString());
    fw.flush();
    fw.close();
}

From source file:com.github.neoio.nio.impl.FileChannelIOStaging.java

public FileChannelIOStaging(File file) throws NetIOException {
    try {/*from   ww w.j a  v  a  2s .  c  o m*/
        this.file = file;
        this.channel = new RandomAccessFile(file, "rw").getChannel();
    } catch (FileNotFoundException e) {
        throw new NetIOException(e);
    }
}

From source file:com.amazonaws.services.s3.internal.FileLocks.java

/**
 * Acquires an exclusive lock on the specified file, creating the file as
 * necessary. Caller of this method is responsible to call the
 * {@link #unlock(File)} method to prevent release leakage.
 * //from   w ww.  jav a 2  s  .  co m
 * @return true if the locking is successful; false otherwise.
 * 
 * @throws FileLockException if we failed to lock the file
 */
public static boolean lock(File file) {
    synchronized (lockedFiles) {
        if (lockedFiles.containsKey(file))
            return false; // already locked
    }
    FileLock lock = null;
    RandomAccessFile raf = null;
    try {
        // Note if the file does not already exist then an attempt will be
        // made to create it because of the use of "rw".
        raf = new RandomAccessFile(file, "rw");
        FileChannel channel = raf.getChannel();
        if (EXTERNAL_LOCK)
            lock = channel.lock();
    } catch (Exception e) {
        IOUtils.closeQuietly(raf, log);
        throw new FileLockException(e);
    }
    final boolean locked;
    synchronized (lockedFiles) {
        RandomAccessFile prev = lockedFiles.put(file, raf);
        if (prev == null) {
            locked = true;
        } else {
            // race condition: some other thread got locked it before this
            locked = false;
            lockedFiles.put(file, prev); // put it back
        }
    }
    if (locked) {
        if (log.isDebugEnabled())
            log.debug("Locked file " + file + " with " + lock);
    } else {
        IOUtils.closeQuietly(raf, log);
    }
    return locked;
}

From source file:ee.ria.xroad.asyncdb.AsyncDBUtil.java

/**
 * Performs task under a lock which applies both to different operations and
 * different threads./* w  ww  . j  av  a2  s .  c  om*/
 *
 * @param task - action to be performed
 * @param lockFilePath - path to file used for process locking
 * @param lockable - object to be synchronized between threads
 * @param <T> - type of result
 * @return - object returned as a result of operation
 * @throws Exception - when locked operation fails or cannot be performed
 */
public static <T> T performLocked(Callable<T> task, String lockFilePath, Object lockable) throws Exception {
    try (RandomAccessFile raf = new RandomAccessFile(lockFilePath, "rw");
            FileOutputStream fos = new FileOutputStream(raf.getFD())) {
        synchronized (lockable) {
            FileLock lock = fos.getChannel().lock();
            try {
                return task.call();
            } finally {
                lock.release();
            }
        }
    }
}

From source file:Main.java

private static byte[] readFileToBuffer(String filePath) {
    File inFile = new File(filePath);
    if (!inFile.exists()) {
        Log.d(TAG, "<readFileToBuffer> " + filePath + " not exists!!!");
        return null;
    }/*from w w w  . j a  va  2  s  .com*/

    RandomAccessFile rafIn = null;
    try {
        rafIn = new RandomAccessFile(inFile, "r");
        int len = (int) inFile.length();
        byte[] buffer = new byte[len];
        rafIn.read(buffer);
        return buffer;
    } catch (IOException e) {
        Log.e(TAG, "<readFileToBuffer> Exception ", e);
        return null;
    } finally {
        try {
            if (rafIn != null) {
                rafIn.close();
                rafIn = null;
            }
        } catch (IOException e) {
            Log.e(TAG, "<readFileToBuffer> close IOException ", e);
        }
    }
}

From source file:io.blobkeeper.file.domain.File.java

public File(java.io.File absolutePath) {
    name = FilenameUtils.getName(absolutePath.getAbsolutePath());

    try {//from   w  w  w  . j  a v  a2 s.  c  o m
        accessFile = new RandomAccessFile(absolutePath, "rw");
        accessFile.seek(0);
        this.channel = accessFile.getChannel();
    } catch (FileNotFoundException e) {
        log.error("Can't open file", e);
        throw new IllegalArgumentException(e);
    } catch (IOException e) {
        log.error("Can't seek file", e);
        throw new IllegalArgumentException(e);
    }
}

From source file:com.linkedin.pinot.perf.BenchmarkFileRead.java

@Setup
public void loadData() {
    try {/*  w  ww  .  j  a  v a  2  s .c  o  m*/
        file = new File("/Users/jfim/index_dir/sTest_OFFLINE/sTest_0_0/daysSinceEpoch.sv.unsorted.fwd");
        raf = new RandomAccessFile(file, "rw");
        length = (int) file.length();
        byteBuffer = ByteBuffer.allocate(length);
        raf.getChannel().read(byteBuffer);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:io.pcp.parfait.dxm.FileByteBufferFactory.java

public ByteBuffer build(int length) throws IOException {
    RandomAccessFile fos = null;//from  w  ww. ja  v  a2s.  co  m
    try {
        File parent = file.getParentFile();
        if (parent == null) {
            throw new RuntimeException("Could not find parent of output file " + file.getCanonicalPath());
        } else if (parent.exists()) {
            file.delete(); /* directory update visible to MMV PMDA */
            if (file.exists()) {
                throw new RuntimeException("Could not delete existing file " + file.getCanonicalPath());
            }
        } else if (!parent.mkdirs()) {
            throw new RuntimeException("Could not create output directory " + parent.getCanonicalPath());
        }
        fos = new RandomAccessFile(file, "rw");
        fos.setLength(length);
        ByteBuffer tempDataFile = fos.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, length);
        tempDataFile.order(ByteOrder.nativeOrder());
        fos.close();

        return tempDataFile;
    } finally {
        if (fos != null) {
            fos.close();
        }
    }
}