Example usage for java.io RandomAccessFile read

List of usage examples for java.io RandomAccessFile read

Introduction

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

Prototype

public int read() throws IOException 

Source Link

Document

Reads a byte of data from this file.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {

    RandomAccessFile raf = new RandomAccessFile("a.dat", "rw");
    int x, y;/*w  ww  .  j a  v a  2 s . co m*/

    for (long i = 0, j = raf.length() - 1; i < j; i++, j--) {
        raf.seek(i);
        x = raf.read();
        raf.seek(j);
        y = raf.read();

        raf.seek(j);
        raf.write(x);
        raf.seek(i);
        raf.write(y);

    }
    raf.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {//w  w  w.jav a 2  s . co  m

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        // write something in the file
        raf.writeUTF("java2s.com Hello World");

        // set the file pointer at 0 position
        raf.seek(0);

        // read the first byte and print it
        System.out.println(raf.read());

        // set the file pointer at 4rth position
        raf.seek(4);

        // read the first byte and print it
        System.out.println(raf.read());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

/** 
 * Read a Pascal string from the file./*  ww w  .j  a  va2 s .  co m*/
 */
public static String readPascalString(RandomAccessFile raf) throws IOException {
    int len = raf.read();
    byte[] buf = new byte[len + 1];
    raf.read(buf, 1, len);
    buf[0] = (byte) len;
    return bytesToPascalString(buf);
}

From source file:Main.java

/**
 *   Reads 4 bytes and concatenates them into a String.
 *   This pattern is used for ID's of various kinds.
 *//*from ww w.ja va 2s  .  c  om*/
public static String read4Chars(RandomAccessFile raf) throws IOException {
    StringBuffer sbuf = new StringBuffer(4);
    for (int i = 0; i < 4; i++) {
        char ch = (char) raf.read();
        sbuf.append(ch);
    }
    return sbuf.toString();
}

From source file:Main.java

/**
 * Reads 4 bytes from file and interprets them as UINT32.<br>
 * /*from ww w . ja v  a 2s. c  om*/
 * @param raf
 *            file to read from.
 * @return UINT32 value
 * @throws IOException
 *             on I/O Errors.
 */
public static long readUINT32(RandomAccessFile raf) throws IOException {
    long result = 0;
    for (int i = 0; i < 4; i++) {
        // Warning, always cast to long here. Otherwise it will be
        // shifted as int, which may produce a negative value, which will
        // then be extended to long and assign the long variable a negative
        // value.
        result <<= 8;
        result |= (long) raf.read();
    }
    return result;
}

From source file:org.ut.biolab.medsavant.shared.util.IOUtils.java

/**
 * Checks if a file is gzipped./* w  w  w. j a v  a2s . c  o  m*/
 *
 * @param f
 * @return
 */
public static boolean isGZipped(File f) {
    int magic = 0;
    try {
        RandomAccessFile raf = new RandomAccessFile(f, "r");
        magic = raf.read() & 0xff | ((raf.read() << 8) & 0xff00);
        raf.close();
    } catch (Throwable e) {
        e.printStackTrace(System.err);
    }
    return magic == GZIPInputStream.GZIP_MAGIC;
}

From source file:VCF.VCF.java

private static boolean isGZipped(File f) throws IOException {
    int magic;//from ww  w  . j a v a  2  s .c  om
    RandomAccessFile raf = new RandomAccessFile(f, "r");
    magic = raf.read() & 0xff | ((raf.read() << 8) & 0xff00);
    raf.close();
    return magic == GZIPInputStream.GZIP_MAGIC;
}

From source file:com.bristle.javalib.io.FileUtil.java

/**************************************************************************
* Copy the contents of the specified binary file to the specified Writer.
*@param  strFilename       Name of the file to copy.
*@param  writer            Writer to write to.
*@throws FileNotFoundException//from  w w w.  j a va2s. c o m
*                           When binary file not found.
*@throws IOException        When an I/O error occurs reading the file or 
*                           writing it to the Writer.
**************************************************************************/
public static void copyBinaryFileToWriter(String strFilename, Writer writer)
        throws FileNotFoundException, IOException {
    RandomAccessFile file = new RandomAccessFile(strFilename, "r");
    try {
        int i;
        while (-1 != (i = file.read())) {
            writer.write((char) i);
        }
        writer.flush();
    } finally {
        file.close();
    }
}

From source file:org.apache.storm.utils.ServerUtils.java

/**
 * Given a zip File input it will return its size
 * Only works for zip files whose uncompressed size is less than 4 GB,
 * otherwise returns the size module 2^32, per gzip specifications
 * @param myFile The zip file as input//from   w  w w .j  a va 2 s.c om
 * @throws IOException
 * @return zip file size as a long
 */
public static long zipFileSize(File myFile) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(myFile, "r");
    raf.seek(raf.length() - 4);
    long b4 = raf.read();
    long b3 = raf.read();
    long b2 = raf.read();
    long b1 = raf.read();
    long val = (b1 << 24) | (b2 << 16) + (b3 << 8) + b4;
    raf.close();
    return val;
}

From source file:cerrla.Performance.java

/**
 * Reads a raw numerical performance file and stores the values as
 * accessible private values.//w w w.  j a v a 2s .  c  o m
 * 
 * @param perfFile
 *            The performance file to read.
 * @return True if the file was read successfully, false otherwise.
 */
public static boolean readRawPerformanceFile(File perfFile, boolean byEpisode) throws Exception {
    if (Config.getInstance().getGeneratorFile() == null) {
        // First, read the last line of the normal file for the time
        RandomAccessFile raf = new RandomAccessFile(perfFile, "r");
        long pos = perfFile.length() - 1;
        StringBuffer line = new StringBuffer();
        char c;
        boolean foundIt = false;
        do {
            raf.seek(pos);
            c = (char) raf.read();
            foundIt |= Character.isDigit(c);
            line.append(c);
            pos--;
        } while (!foundIt || Character.isDigit(c) || c == ':');
        raf.close();
        String time = line.reverse().toString().trim();
        String[] timeSplit = time.split(":");
        runTime_ = (Long.parseLong(timeSplit[2]) + 60 * Long.parseLong(timeSplit[1])
                + 3600 * Long.parseLong(timeSplit[0])) * 1000;
    }

    if (Config.getInstance().getGeneratorFile() == null)
        perfFile = new File(perfFile.getPath() + "raw");
    else
        perfFile = new File(perfFile.getPath() + "greedy");
    performanceMap_ = new TreeMap<Integer, Float[]>();
    FileReader reader = new FileReader(perfFile);
    BufferedReader buf = new BufferedReader(reader);

    // For every value within the performance file
    String input = null;
    Float[] prevPerfs = null;
    while ((input = buf.readLine()) != null) {
        String[] vals = input.split("\t");
        if (vals[PerformanceDetails.EPISODE.ordinal()].equals("Episode"))
            continue;

        Float[] perfs = new Float[PerformanceDetails.values().length];
        int episode = 0;
        for (PerformanceDetails detail : PerformanceDetails.values()) {
            if (vals.length > detail.ordinal()) {
                if (!vals[detail.ordinal()].equals("null"))
                    perfs[detail.ordinal()] = Float.parseFloat(vals[detail.ordinal()]);
                else if (detail.equals(PerformanceDetails.ELITEMEAN)
                        && !vals[PerformanceDetails.ELITEMAX.ordinal()].equals("null"))
                    perfs[detail.ordinal()] = Float.parseFloat(vals[PerformanceDetails.ELITEMAX.ordinal()]);
                else if (detail.equals(PerformanceDetails.ELITEMEAN)
                        || detail.equals(PerformanceDetails.ELITEMAX))
                    perfs[detail.ordinal()] = Float.parseFloat(vals[PerformanceDetails.MEAN.ordinal()]);
                else if (prevPerfs != null)
                    perfs[detail.ordinal()] = prevPerfs[detail.ordinal()];
            }

            if (detail.equals(PerformanceDetails.EPISODE))
                episode = perfs[detail.ordinal()].intValue();
        }

        performanceMap_.put(episode, perfs);
        prevPerfs = perfs;
    }

    buf.close();
    reader.close();

    return true;
}