Java File Touch touch(File file)

Here you can find the source of touch(File file)

Description

Updates a file's last-modified timestamp.

License

LGPL

Parameter

Parameter Description
file The file

Exception

Parameter Description
IOException In case of an I/O error

Declaration

public static void touch(File file) throws IOException 

Method Source Code

//package com.java2s;
/**//from  w w  w .  j  av a  2s.co m
 * Copyright 2011-2016 Three Crickets LLC.
 * <p>
 * The contents of this file are subject to the terms of the LGPL version 3.0:
 * http://www.gnu.org/copyleft/lesser.html
 * <p>
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly from Three Crickets
 * at http://threecrickets.com/
 */

import java.io.File;

import java.io.IOException;

public class Main {
    /**
     * Updates a file's last-modified timestamp.
     * <p>
     * If the file doesn't exist, it is created as an empty file, including
     * necessary parent directories.
     * 
     * @param file
     *        The file
     * @throws IOException
     *         In case of an I/O error
     */
    public static void touch(File file) throws IOException {
        if (file.exists())
            file.setLastModified(System.currentTimeMillis());
        else {
            File parent = file.getParentFile();
            if (parent != null)
                parent.mkdirs();
            file.createNewFile();
        }
    }
}

Related

  1. touch(File file)
  2. touch(File file)
  3. touch(File file)
  4. touch(File file)
  5. touch(File file)
  6. touch(File file)
  7. touch(File file)
  8. touch(File file)
  9. touch(File file)