"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) 
            if(!parent.exists())
	        if(!parent.mkdirs())
	            throw new IOException("Cannot create parent
                    directories for file: " + file);
		
		file.createNewFile();
    }
		
    boolean success = file.setLastModified(System.currentTimeMillis());
    if (!success)
        throw new IOException("Unable to set the last modification time for " + file);
}