Example usage for java.io RandomAccessFile length

List of usage examples for java.io RandomAccessFile length

Introduction

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

Prototype

public native long length() throws IOException;

Source Link

Document

Returns the length of this file.

Usage

From source file:Main.java

public static byte[] readFile(File f1) throws IOException {
    RandomAccessFile f = new RandomAccessFile(f1, "r");
    byte[] b = new byte[(int) f.length()];
    f.read(b);/* ww w  . j a  v a 2  s  .c  om*/
    f.close();
    return b;
}

From source file:Main.java

private static String readSaltFromFile(File file) throws IOException {
    RandomAccessFile accessFile = new RandomAccessFile(file, "r");
    byte[] bs = new byte[(int) accessFile.length()];
    accessFile.readFully(bs);//from  ww  w .ja v  a  2 s  .  c  o m
    accessFile.close();
    return new String(bs);
}

From source file:Main.java

private static String readInstallationFile(File installation) throws IOException {
    RandomAccessFile f = new RandomAccessFile(installation, "r");
    byte[] bytes = new byte[(int) f.length()];
    f.readFully(bytes);/*from  www .  j  av a 2s  . c o m*/
    f.close();
    return new String(bytes);
}

From source file:Main.java

public static long truncateFile(String fileName, long size) throws FileNotFoundException, IOException {
    RandomAccessFile raf = new RandomAccessFile(fileName, "rw");

    if (raf.length() >= size) {
        FileChannel channel = raf.getChannel();
        channel.truncate(size);/*  www  .  j  av a2 s  . co  m*/
        return size;
    }

    return raf.length();
}

From source file:Main.java

private static String readInstallationFile(File installation) throws IOException {
    RandomAccessFile accessFile = new RandomAccessFile(installation, "r");
    byte[] bs = new byte[(int) accessFile.length()];
    accessFile.readFully(bs);//from   w  ww  . ja v  a  2  s  .  c  om
    accessFile.close();
    return new String(bs);
}

From source file:Main.java

public static boolean appendFile(String filename, byte[] data, int datapos, int datalength) {
    try {//from  w  w w . j  av a 2 s.co  m

        createFile(filename);

        RandomAccessFile rf = new RandomAccessFile(filename, "rw");
        rf.seek(rf.length());
        rf.write(data, datapos, datalength);
        if (rf != null) {
            rf.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return true;
}

From source file:Main.java

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

    try {/*from   w  w  w .j a v a2 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:org.apache.hadoop.hdfs.server.namenode.FSImageUtil.java

public static boolean checkFileFormat(RandomAccessFile file) throws IOException {
    if (file.length() < Loader.MINIMUM_FILE_LENGTH)
        return false;

    byte[] magic = new byte[MAGIC_HEADER.length];
    file.readFully(magic);//from w w  w . j av a2s  .co m
    if (!Arrays.equals(MAGIC_HEADER, magic))
        return false;

    return true;
}

From source file:Main.java

public static byte[] readFile(File file) throws IOException {
    // Open file/*  ww w  .  j  av a2  s. c o m*/
    RandomAccessFile f = new RandomAccessFile(file, "r");
    try {
        // Get and check length
        long longlength = f.length();
        int length = (int) longlength;
        if (length != longlength)
            throw new IOException("File size >= 2 GB");
        // Read file and return data
        byte[] data = new byte[length];
        f.readFully(data);
        return data;
    } finally {
        f.close();
    }
}

From source file:org.snt.inmemantlr.utils.FileUtils.java

/**
 * load file content//from  w w  w.ja  va 2s  .c om
 *
 * @param path path of the file to load
 * @return file content as string
 */
public static String loadFileContent(String path) {
    byte[] bytes;
    try {
        RandomAccessFile f = new RandomAccessFile(path, "r");
        bytes = new byte[(int) f.length()];
        f.read(bytes);
    } catch (Exception e) {
        return null;
    }
    return new String(bytes);
}