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 byte[] readFile(File f1) throws IOException {
    RandomAccessFile f = new RandomAccessFile(f1, "r");
    byte[] b = new byte[(int) f.length()];
    f.read(b);/*from   w w w . j  a  v a 2  s.  c  o m*/
    f.close();
    return b;
}

From source file:Main.java

public static boolean createEmptyFile(String path, long size) throws IOException {
    File file = new File(path);
    File parent = file.getParentFile();
    parent.mkdirs();// w w w.  j  a v a 2 s .co  m
    RandomAccessFile raf = new RandomAccessFile(file, "rw");
    raf.setLength(size);
    raf.close();
    return true;
}

From source file:Main.java

public static void readStreamToFile(InputStream inStream, String filePath) throws Exception {
    File file = new File(filePath + ".wei");
    RandomAccessFile outStream = new RandomAccessFile(file, "rw");
    outStream.seek(0);//from  w  w  w.j a v a 2s. c  om
    byte[] buffer = new byte[1024];
    int len = -1;
    while ((len = inStream.read(buffer)) != -1) {
        outStream.write(buffer, 0, len);
    }
    outStream.close();
    inStream.close();
    file.renameTo(new File(filePath));
    return;
}

From source file:Main.java

public static boolean createEmptyFile(String path, long size) throws IOException {
    File file = new File(path);
    File parent = file.getParentFile();
    parent.mkdirs();// ww  w.ja  va 2s .  co  m
    RandomAccessFile raf = null;
    raf = new RandomAccessFile(file, "rw");
    raf.setLength(size);
    raf.close();
    return true;
}

From source file:Main.java

private static byte[] readFile2Bytes(final File file) {
    FileChannel fc = null;/*from  www .jav a  2  s  . c  o m*/
    try {
        fc = new RandomAccessFile(file, "r").getChannel();
        int size = (int) fc.size();
        MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, 0, size).load();
        byte[] data = new byte[size];
        mbb.get(data, 0, size);
        return data;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (fc != null) {
                fc.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:Main.java

public static RandomAccessFile U7open2(String nm1, String nm2) {
    String nm = U7exists(nm1);//w w w.j a  v a  2  s .  c  o m
    if (nm != null)
        try {
            return new RandomAccessFile(nm, "r");
        } catch (IOException e) {
        }
    nm = U7exists(nm2);
    if (nm != null)
        try {
            return new RandomAccessFile(nm, "r");
        } catch (IOException e) {
        }
    return null;
}

From source file:Main.java

public static ByteBuffer fromFile(File file) throws IOException {
    RandomAccessFile raf = null;/*from   www .  j a  v a2 s. c  o m*/
    FileChannel channel = null;
    try {
        raf = new RandomAccessFile(file, "r");
        channel = raf.getChannel();
        return channel.map(FileChannel.MapMode.READ_ONLY, 0, file.length()).load();
    } finally {
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                // Ignored.
            }
        }
        if (raf != null) {
            try {
                raf.close();
            } catch (IOException e) {
                // Ignored.
            }
        }
    }
}

From source file:Main.java

public static void toFile(ByteBuffer buffer, File file) throws IOException {
    RandomAccessFile raf = null;/*  w ww  .ja  v a 2 s  . com*/
    FileChannel channel = null;
    try {
        raf = new RandomAccessFile(file, "rw");
        channel = raf.getChannel();
        channel.write(buffer);
        channel.force(false /*metadata*/);
        channel.close();
        raf.close();
    } finally {
        if (channel != null) {
            try {
                channel.close();
            } catch (IOException e) {
                // Ignored.
            }
        }
        if (raf != null) {
            try {
                raf.close();
            } catch (IOException e) {
                // Ignored.
            }
        }
    }
}

From source file:Main.java

public static RandomAccessFile U7open(String nm, boolean hardfail) throws IOException {
    String fname = U7exists(nm);/* w w w .  j  a va  2s.  c  o m*/
    if (fname != null)
        try {
            return new RandomAccessFile(fname, "r");
        } catch (IOException e) {
            if (hardfail)
                throw e;
        }
    return null;
}

From source file:Main.java

public static void readFile(String fileName) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
    int counter = raf.readInt();
    String msg = raf.readUTF();//from w  w  w.j  a  v  a  2 s .  c  o  m

    System.out.println(counter);
    System.out.println(msg);
    incrementReadCounter(raf);
    raf.close();
}