FileDocCategorySizeDatePackage
FileUtils.javaAPI DocAndroid 1.5 API2692Wed May 06 22:41:02 BST 2009com.android.dx.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 non-null; name of the file to read
return
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 non-null; the file to read
return
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;