Example usage for java.io RandomAccessFile close

List of usage examples for java.io RandomAccessFile close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Document

Closes this random access file stream and releases any system resources associated with the stream.

Usage

From source file:Main.java

public static void initialWrite(String fileName) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
    raf.writeInt(0);/* w w w.  j  a  v  a  2  s  .c  o m*/
    raf.writeUTF("Hello world!");
    raf.close();
}

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();//from w w  w.j  a v a2 s  .  c  o m
    RandomAccessFile raf = null;
    raf = new RandomAccessFile(file, "rw");
    raf.setLength(size);
    raf.close();
    return true;
}

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   ww  w .ja  v  a 2 s  .  c om*/
    f.close();
    return b;
}

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);// w  ww.j a v a2s .  co m
    f.close();
    return new String(bytes);
}

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 va 2s  .  com
    accessFile.close();
    return new String(bs);
}

From source file:Main.java

public static void WriteTxtFile(Context context, String strcontent) {

    String strContent = strcontent + "\n";
    try {//from w ww  . j av  a2s.co  m
        File file = new File(context.getCacheDir() + File.separator + "wanjia.txt");
        if (!file.exists()) {
            file.createNewFile();
        }
        RandomAccessFile raf = new RandomAccessFile(file, "rw");
        raf.seek(file.length());
        raf.write(strContent.getBytes());
        raf.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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  w w .j  a 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 a  v  a2 s . 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;
}

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();/*  w w w.j a  va2 s . com*/

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

From source file:Main.java

public static byte[] readFromFile(String fileName, int offset, int len) {
    if (fileName == null) {
        return null;
    }//from   w  ww. j  av  a2s . c  om

    File file = new File(fileName);
    if (!file.exists()) {
        //WLog.i(FileUtils.class, "readFromFile: file not found");
        return null;
    }

    if (len == -1) {
        len = (int) file.length();
    }

    if (offset < 0) {
        //WLog.e(FileUtils.class, "readFromFile invalid offset:" + offset);
        return null;
    }
    if (len <= 0) {
        //WLog.e(FileUtils.class, "readFromFile invalid len:" + len);
        return null;
    }
    if (offset + len > (int) file.length()) {
        //WLog.e(FileUtils.class, "readFromFile invalid file len:" + file.length());
        return null;
    }

    byte[] b = null;
    try {
        RandomAccessFile in = new RandomAccessFile(fileName, "r");
        b = new byte[len];
        in.seek(offset);
        in.readFully(b);
        in.close();

    } catch (Exception e) {
        // WLog.e(FileUtils.class, "readFromFile : errMsg = " + e.getMessage());
        e.printStackTrace();
    }
    return b;
}