Updates the timestamp of the given file to the current time. - Android File Input Output

Android examples for File Input Output:File Name

Description

Updates the timestamp of the given file to the current time.

Demo Code


//package com.java2s;
import android.content.Context;

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

public class Main {
    /**//from w w  w . j a  va 2 s .  c o m
     * Updates the timestamp of the given file to the current time.
     * Taken from http://www.intransitione.com/blog/touch-a-file-on-android/
     */
    public static void touch(File file, Context cxt) throws IOException {
        if (!file.exists()) {
            File parent = file.getParentFile();
            if (parent != null)
                if (!parent.exists())
                    if (!parent.mkdirs())
                        throw new IOException(
                                "Cannot create parent directories for file: "
                                        + file);

            file.createNewFile();
            FileOutputStream out = cxt.openFileOutput(file.getName(),
                    Context.MODE_WORLD_READABLE);
            out.close();
        }

        boolean success = file.setLastModified(System.currentTimeMillis());
        if (!success)
            throw new IOException(
                    "Unable to set the last modification time for " + file);
    }
}

Related Tutorials