Read a long value from the specified file - Android File Input Output

Android examples for File Input Output:InputStream

Description

Read a long value from the specified file

Demo Code


//package com.java2s;

import java.io.Closeable;
import java.io.DataInputStream;

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.io.InputStream;

public class Main {
    /**//from   w  w  w  .  j  a  v a2  s .co m
     * Read a long value from the specified file
     * 
     * @param file
     *            The target file
     * @return A long value
     * @throws IOException
     */
    public static long readAsLong(final File file) throws IOException {
        return readAsLong(file.getAbsolutePath());
    }

    /**
     * Read a long value from the specified path
     * 
     * @param path
     *            The target file path
     * @return A long value
     * @throws IOException
     */
    public static long readAsLong(final String path) throws IOException {
        FileInputStream fis = null;

        try {
            return readAsLong(fis = new FileInputStream(path));
        } finally {
            closeQuietly(fis);
        }
    }

    /**
     * Read a long value from the specified input stream
     * 
     * @param is
     *            The target input stream
     * @return A long value
     * @throws IOException
     */
    public static long readAsLong(final InputStream is) throws IOException {
        return new DataInputStream(is).readLong();
    }

    /**
     * Close the closeables quietly
     * 
     * @param closeables
     *            Instances of {@link Closeable}
     */
    public static void closeQuietly(final Closeable... closeables) {
        if (null == closeables || closeables.length <= 0)
            return;

        for (int i = 0; i < closeables.length; i++) {
            final Closeable closeable = closeables[i];
            if (null == closeable)
                continue;

            try {
                closeables[i].close();
            } catch (IOException e) {
            }
        }
    }
}

Related Tutorials