Java RandomAccessFile Read load(String f)

Here you can find the source of load(String f)

Description

Loads an entire file from the filesystem.

License

Apache License

Parameter

Parameter Description
f the file name to load

Return

the bytes of the file or an empty array

Declaration

public static byte[] load(String f) 

Method Source Code

//package com.java2s;
/*/*  w w w.j  a va 2s .  co m*/
 * Copyright 2011 David Karnok
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;

import java.io.IOException;

import java.io.RandomAccessFile;

public class Main {
    /**
     * Loads an entire file from the filesystem.
     * @param f the file to load
     * @return the bytes of file or an empty array
     */
    public static byte[] load(File f) {
        if (f.canRead()) {
            byte[] buffer = new byte[(int) f.length()];
            try {
                RandomAccessFile fin = new RandomAccessFile(f, "r");
                try {
                    fin.readFully(buffer);
                    return buffer;
                } finally {
                    fin.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            System.err.println("File inaccessible: " + f);
        }
        return new byte[0];
    }

    /**
     * Loads an entire file from the filesystem.
     * @param f the file name to load
     * @return the bytes of the file or an empty array
     */
    public static byte[] load(String f) {
        return load(new File(f));
    }
}

Related

  1. appendFile(RandomAccessFile main, RandomAccessFile extra)
  2. load(String fn, int type, int m, int n)
  3. LoadDspaceMapFile(String filename)
  4. LoadFloatFile(String filename)
  5. LoadGhostMapFile(String filename, int n_ids, int n_ghosts)