Write a long value into the specified file - Android File Input Output

Android examples for File Input Output:InputStream

Description

Write a long value into the specified file

Demo Code


//package com.java2s;

import java.io.Closeable;

import java.io.DataOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    /**//  w  ww. j  a v a2  s  .  c om
     * Write a long value into the specified file
     * 
     * @param file
     *            The target file
     * @param data
     *            The long value
     * @throws IOException
     */
    public static void writeLong(final File file, final long data)
            throws IOException {
        writeLong(file.getAbsolutePath(), data);
    }

    /**
     * Write a long value into the specified path.
     * 
     * @param path
     *            The target file path
     * @param data
     *            The long value
     * @throws IOException
     */
    public static void writeLong(final String path, final long data)
            throws IOException {
        DataOutputStream dos = null;

        try {
            dos = new DataOutputStream(new FileOutputStream(path));
            dos.writeLong(data);
            dos.flush();
        } finally {
            closeQuietly(dos);
        }
    }

    /**
     * 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