"touch" a file on Android

Posted by Marco Dinacci on 0 comments
This snippet mimics the behaviour of the unix touch command on the Android platform:
public static void touch(File file) throws IOException {
    if(!file.exists()) {
        File parent = file.getParentFile();
        if(parent != null) 
	            throw new IOException("Cannot create parent
                    directories for file: " + file);
    boolean success = file.setLastModified(System.currentTimeMillis());
    if (!success)
        throw new IOException("Unable to set the last modification time for " + file);