Reads 4 bytes from file and interprets them as UINT32 from RandomAccessFile. - Java File Path IO

Java examples for File Path IO:RandomAccessFile

Description

Reads 4 bytes from file and interprets them as UINT32 from RandomAccessFile.

Demo Code


//package com.java2s;
import java.io.IOException;
import java.io.RandomAccessFile;

public class Main {
    /**/*from   www.  jav  a  2  s  .  c  o  m*/
     * Reads 4 bytes from file and interprets them as UINT32.<br>
     * 
     * @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;
    }
}

Related Tutorials