Example usage for java.io RandomAccessFile close

List of usage examples for java.io RandomAccessFile close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this random access file stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

public static String read(String s) throws IOException {
    String s1;//from  w ww . j a va2 s. c  o m
    RandomAccessFile randomaccessfile;
    s1 = "";
    randomaccessfile = null;
    File file = new File(s);
    randomaccessfile = new RandomAccessFile(file, "r");
    s1 = randomaccessfile.readLine();
    randomaccessfile.close();
    return s1;
}

From source file:Main.java

public static byte[] readFromFile(String fileName, int offset, int len) {
    if (fileName == null) {
        return null;
    }/*from w  ww . j  ava  2s  . c o  m*/

    File file = new File(fileName);
    if (!file.exists()) {
        Log.i(TAG, "readFromFile: file not found");
        return null;
    }

    if (len == -1) {
        len = (int) file.length();
    }

    Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len));

    if (offset < 0) {
        Log.e(TAG, "readFromFile invalid offset:" + offset);
        return null;
    }
    if (len <= 0) {
        Log.e(TAG, "readFromFile invalid len:" + len);
        return null;
    }
    if (offset + len > (int) file.length()) {
        Log.e(TAG, "readFromFile invalid file len:" + file.length());
        return null;
    }

    byte[] b = null;
    try {
        RandomAccessFile in = new RandomAccessFile(fileName, "r");
        b = new byte[len];
        in.seek(offset);
        in.readFully(b);
        in.close();

    } catch (Exception e) {
        Log.e(TAG, "readFromFile : errMsg = " + e.getMessage());
        e.printStackTrace();
    }
    return b;
}

From source file:Main.java

public static byte[] readFromFile(String fileName, int offset, int len) {
    if (fileName == null) {
        return null;
    }/*from   ww  w.j  av a2s  .co  m*/

    File file = new File(fileName);
    if (!file.exists()) {
        Log.i(TAG, "readFromFile: file not found");
        return null;
    }

    if (len == -1) {
        len = (int) file.length();
    }

    Log.d(TAG, "readFromFile : offset = " + offset + " len = " + len + " offset + len = " + (offset + len));

    if (offset < 0) {
        Log.e(TAG, "readFromFile invalid offset:" + offset);
        return null;
    }
    if (len <= 0) {
        Log.e(TAG, "readFromFile invalid len:" + len);
        return null;
    }
    if (offset + len > (int) file.length()) {
        Log.e(TAG, "readFromFile invalid file len:" + file.length());
        return null;
    }

    byte[] b = null;
    try {
        RandomAccessFile in = new RandomAccessFile(fileName, "r");
        b = new byte[len]; //
        in.seek(offset);
        in.readFully(b);
        in.close();

    } catch (Exception e) {
        Log.e(TAG, "readFromFile : errMsg = " + e.getMessage());
        e.printStackTrace();
    }
    return b;
}

From source file:com.wabacus.util.FileLockTools.java

private static boolean release(RandomAccessFile raf) {
    try {/*  w  w  w .j  a  v a  2  s. c o  m*/
        if (raf != null) {
            raf.close();
            raf = null;
        }
        return true;
    } catch (Exception e) {
        System.out.println("RandomAccessFile");
        e.printStackTrace();
        return false;
    }
}

From source file:Main.java

public static void readStreamToFile(InputStream inStream, String filePath) throws Exception {
    File file = new File(filePath + ".wei");
    RandomAccessFile outStream = new RandomAccessFile(file, "rw");
    outStream.seek(0);//from ww  w .jav  a  2  s  .  co  m
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    inStream.close();
    file.renameTo(new File(filePath));
    return;
}

From source file:de.huxhorn.sulky.io.IOUtilities.java

/**
 * Unconditionally close a <code>RandomAccessFile</code>.
 * <p>/*from  w  w w  . jav a  2s  .  com*/
 * Equivalent to {@link RandomAccessFile#close()}, except any exceptions will be ignored.
 * {@link InterruptedIOException} is handled correctly by {@link #interruptIfNecessary(Throwable)}.
 * This is typically used in finally blocks.
 *
 * @param x  the RandomAccessFile to close, may be null or already closed
 */
public static void closeQuietly(RandomAccessFile x) {
    if (x == null) {
        return;
    }
    try {
        x.close();
    } catch (IOException e) {
        interruptIfNecessary(e);
    }
}

From source file:Main.java

public static short[] fromAudioFile(RandomAccessFile f) {
    short[] samples = null;

    try {/*from   www. ja  va  2  s  .  c o m*/
        samples = new short[(int) (f.length() / 2)];
        f.seek(0); //Seek to start point of file

        for (int i = 0; i < f.length() / 2; i++) {
            samples[i] = f.readShort();
        }
        f.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return samples;
}

From source file:Main.java

public static void toFile(ByteBuffer buffer, File file) throws IOException {
    RandomAccessFile raf = null;
    FileChannel channel = null;//  ww  w  .j a v a2 s  .co  m
    try {
        raf = new RandomAccessFile(file, "rw");
        channel = raf.getChannel();
        channel.write(buffer);
        channel.force(false /*metadata*/);
        channel.close();
        raf.close();
    } finally {
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                // Ignored.
            }
        }
        if (raf != null) {
            try {
                raf.close();
            } catch (IOException e) {
                // Ignored.
            }
        }
    }
}

From source file:cn.edu.xmu.tidems.control.support.TideMSUtil.java

public static boolean isHDF5File(final File file) {

    if ((!file.exists()) || (file.isDirectory()) || (file.length() < 1024)) {
        return false;
    }//  w w  w . j  a  va  2 s .c  o m
    try {
        final RandomAccessFile raf = new RandomAccessFile(file, "r");
        final long sig = raf.readLong(); // The first 8 byte are format signatures of hdf5 file
        raf.close();
        return sig == -8554512424533091830L; // 0x89 48 44 46 0d 0a 1a 0a
    } catch (final Exception e) {
        return false;
    }
}

From source file:net.timewalker.ffmq4.storage.data.impl.BlockBasedDataStoreTools.java

private static void initDataFile(File dataFile, int blockCount, int blockSize, boolean forceSync)
        throws DataStoreException {
    log.debug("Creating an empty map file (size=" + blockCount + "x" + blockSize + ") ...");

    // Create an empty file
    try {// w  w w .  j a va2  s.  co  m
        RandomAccessFile dataFileMap = new RandomAccessFile(dataFile, "rw");
        dataFileMap.setLength((long) blockSize * blockCount);
        if (forceSync)
            dataFileMap.getFD().sync();
        dataFileMap.close();
    } catch (IOException e) {
        throw new DataStoreException("Cannot initialize map file " + dataFile.getAbsolutePath(), e);
    }
}