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(byte b[]) throws IOException 

Source Link

Document

Reads up to b.length bytes of data from this file into an array of bytes.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {/*w ww.j a  v a  2s  . co m*/
        byte[] b1 = { 1, 2, 3 };
        byte[] b2 = { 1, 2, 3, 4, 5, 6, 7, 8 };

        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 8 bytes and print the number of bytes read
        System.out.println(raf.read(b1));

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

        // read the first 8 bytes and print the number of bytes read
        System.out.println(raf.read(b2));
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    RandomAccessFile randomAccessFile = null;

    String line1 = "line\n";
    String line2 = "asdf1234\n";

    // read / write permissions
    randomAccessFile = new RandomAccessFile("yourFile.dat", "rw");

    randomAccessFile.writeBytes(line1);//from  w w  w. j a  v a 2  s  .  com
    randomAccessFile.writeBytes(line2);

    // Place the file pointer at the end of the first line
    randomAccessFile.seek(line1.length());

    byte[] buffer = new byte[line2.length()];
    randomAccessFile.read(buffer);
    System.out.println(new String(buffer));

    randomAccessFile.close();
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    RandomAccessFile randomAccessFile = null;

    String line1 = "java2s.com\n";
    String line2 = "asdf1234\n";

    // read / write permissions
    randomAccessFile = new RandomAccessFile("yourFile.dat", "rw");

    randomAccessFile.writeBytes(line1);//from   w ww  .j  a v  a 2  s  .  co  m
    randomAccessFile.writeBytes(line2);

    // Place the file pointer at the end of the first line
    randomAccessFile.seek(line1.length());

    byte[] buffer = new byte[line2.length()];
    randomAccessFile.read(buffer);
    System.out.println(new String(buffer));

    randomAccessFile.close();
}

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);
    f.close();/*from w w  w.j  a  va2  s.c o m*/
    return b;
}

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

/**
 * load file content//from  w  w w. ja  va  2  s.  c o m
 *
 * @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);
}

From source file:Main.java

public static String getCPUInfo() {
    RandomAccessFile reader = null;
    try {/*from w w w.  ja v  a 2  s. 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 byte[] readFileToBuffer(String filePath) {
    File inFile = new File(filePath);
    if (!inFile.exists()) {
        Log.d(TAG, "<readFileToBuffer> " + filePath + " not exists!!!");
        return null;
    }/*from   w w w .  jav a 2  s .  c o  m*/

    RandomAccessFile rafIn = null;
    try {
        rafIn = new RandomAccessFile(inFile, "r");
        int len = (int) inFile.length();
        byte[] buffer = new byte[len];
        rafIn.read(buffer);
        return buffer;
    } catch (IOException e) {
        Log.e(TAG, "<readFileToBuffer> Exception ", e);
        return null;
    } finally {
        try {
            if (rafIn != null) {
                rafIn.close();
                rafIn = null;
            }
        } catch (IOException e) {
            Log.e(TAG, "<readFileToBuffer> close IOException ", e);
        }
    }
}

From source file:net.yacy.document.parser.tarParser.java

public final static boolean isTar(File f) {
    if (!f.exists() || f.length() < 0x105)
        return false;
    RandomAccessFile raf = null;
    try {//from  www .  j a va2s .c om
        raf = new RandomAccessFile(f, "r");
        raf.seek(0x101);
        byte[] b = new byte[5];
        raf.read(b);
        return MAGIC.equals(UTF8.String(b));
    } catch (final FileNotFoundException e) {
        return false;
    } catch (final IOException e) {
        return false;
    } finally {
        if (raf != null)
            try {
                raf.close();
            } catch (final IOException e) {
            }
    }
}

From source file:Main.java

public static byte[] getAsBinary(String key) {
    RandomAccessFile RAFile = null;
    boolean removeFile = false;
    try {/*from  ww w.  j ava  2 s.  co  m*/
        File file = new File(cacheDir, key);
        if (!file.exists())
            return null;
        RAFile = new RandomAccessFile(file, "r");
        byte[] byteArray = new byte[(int) RAFile.length()];
        RAFile.read(byteArray);
        return byteArray;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        if (RAFile != null) {
            try {
                RAFile.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

From source file:se.kth.hopsworks.util.CharonOperations.java

/**
 * Share a repository with other site_ids
 *
 * @param path - the path to the repository to be shared (the repository should be empty)
 * @param permissions - the desired permissions ('r' or 'rw')
 * @param granteeId - the id of the grantee
 * @return - a share_token to share with the grantees site_ids
 *//*  ww  w  . j  a  v a2  s  .  co m*/
public static String share(String path, String permissions, int granteeId) throws Exception {

    if (!path.startsWith("/"))
        path = File.separator + path;

    try {
        String ACLcommand = "setfacl -m u:" + granteeId + ":" + permissions + " " + charonMountPointPath + path;
        Process p = Runtime.getRuntime().exec(ACLcommand);
        p.waitFor();

        File file = new File(CHARON_PATH);
        byte[] data = null;
        for (File f : file.listFiles()) {
            if (f.getName().startsWith("share")) {
                RandomAccessFile rd = new RandomAccessFile(f, "rw");
                data = new byte[(int) f.length()];
                rd.read(data);

                f.delete();
                if (data != null) {
                    break;
                }
                rd.close();
            }
        }
        if (data == null) {
            return "";
        }
        return new String(data);
    } catch (IOException e) {
        throw new Exception("Something went wrong!");
    } catch (InterruptedException e) {
        throw new Exception("Something went wrong!");
    }
}