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:Main.java

public static void append(String fileName, String text) throws Exception {
    File f = new File(fileName);
    long fileLength = f.length();
    RandomAccessFile raf = new RandomAccessFile(f, "rw");
    raf.seek(fileLength);/*from ww w  .  java 2 s. c  om*/
    raf.writeBytes(text);
    raf.close();
}

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   w  w  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 accessFile = new RandomAccessFile(installation, "r");
    byte[] bs = new byte[(int) accessFile.length()];
    accessFile.readFully(bs);//ww w . j a  v a  2 s .com
    accessFile.close();
    return new String(bs);
}

From source file:Cat.java

public static void concatenate(String fileName) {
    RandomAccessFile file = null;
    String line = null;/*from  w  w w .j a  v a  2s  .  c  o  m*/

    try {
        file = new RandomAccessFile(fileName, "r");
        while ((line = file.readLine()) != null) {
            System.out.println(line);
        }
        return;
    } catch (FileNotFoundException fnf) {
        System.err.println("File: " + fileName + " not found.");
    } catch (Exception e) {
        System.err.println(e.toString());
    } finally {
        if (file != null) {
            try {
                file.close();
            } catch (IOException io) {
            }
        }
    }

}

From source file:Main.java

public static byte[] readFile(File file) throws IOException {
    // Open file//  www . ja  v a 2 s  .  co  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:Main.java

public static String getCPUInfo() {
    RandomAccessFile reader = null;
    try {//from w  w  w  .  j  a va 2s.  co m
        byte[] bs = new byte[1024];
        reader = new RandomAccessFile("/proc/cpuinfo", "r");
        reader.read(bs);
        String ret = new String(bs);
        int index = ret.indexOf(0);
        return index != -1 ? ret.substring(0, index) : ret;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

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);//www  . j a v a2  s  . c o m
    f.close();
    return new String(bytes);
}

From source file:Main.java

public static String read(String s) throws IOException {
    String s1;//from ww  w.j av  a  2s.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

@SuppressWarnings("resource")
public static String getTotalRAM() {
    RandomAccessFile reader = null;
    String load = null;// w  ww. j  a  v  a2s.  co m
    try {
        reader = new RandomAccessFile("/proc/meminfo", "r");
        load = reader.readLine();
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        // Streams.close(reader);
    }

    return load.replaceAll("MemTotal:", "").replaceAll("kB", "").trim();
}

From source file:Main.java

public static boolean appendFile(String filename, byte[] data, int datapos, int datalength) {
    try {// ww  w.jav a2s.c o  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;
}