Write an integer value into the specified file. - Android File Input Output

Android examples for File Input Output:InputStream

Description

Write an integer 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 {
    /**// ww w.  j a va 2 s  .  c o m
     * Write an integer value into the specified file.
     * 
     * @param file
     *            The target file
     * @param data
     *            The integer value
     * @throws IOException
     */
    public static void writeInt(final File file, final int data)
            throws IOException {
        writeInt(file.getAbsolutePath(), data);
    }

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

        try {
            dos = new DataOutputStream(new FileOutputStream(path));
            dos.writeInt(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