FileDocCategorySizeDatePackage
FileUtils.javaAPI DocAndroid 5.1 API2726Thu Mar 12 22:18:30 GMT 2015com.android.dexgen.util

FileUtils

public final class FileUtils extends Object
File I/O utilities.

Fields Summary
Constructors Summary
private FileUtils()
This class is uninstantiable.

        // This space intentionally left blank.
    
Methods Summary
public static byte[]readFile(java.lang.String fileName)
Reads the named file, translating {@link IOException} to a {@link RuntimeException} of some sort.

param
fileName {@code non-null;} name of the file to read
return
{@code non-null;} contents of the file

        File file = new File(fileName);
        return readFile(file);
    
public static byte[]readFile(java.io.File file)
Reads the given file, translating {@link IOException} to a {@link RuntimeException} of some sort.

param
file {@code non-null;} the file to read
return
{@code non-null;} contents of the file

        if (!file.exists()) {
            throw new RuntimeException(file + ": file not found");
        }

        if (!file.isFile()) {
            throw new RuntimeException(file + ": not a file");
        }

        if (!file.canRead()) {
            throw new RuntimeException(file + ": file not readable");
        }

        long longLength = file.length();
        int length = (int) longLength;
        if (length != longLength) {
            throw new RuntimeException(file + ": file too long");
        }

        byte[] result = new byte[length];

        try {
            FileInputStream in = new FileInputStream(file);
            int at = 0;
            while (length > 0) {
                int amt = in.read(result, at, length);
                if (amt == -1) {
                    throw new RuntimeException(file + ": unexpected EOF");
                }
                at += amt;
                length -= amt;
            }
            in.close();
        } catch (IOException ex) {
            throw new RuntimeException(file + ": trouble reading", ex);
        }

        return result;