Java File Touch touch(final File f)

Here you can find the source of touch(final File f)

Description

Touch the specified file, creating if necessary.

License

Apache License

Declaration

public static void touch(final File f) throws IOException 

Method Source Code

//package com.java2s;
/*/*ww w  . ja  va2s  .  c  om*/
The OpenTRV project licenses this file to you
under the Apache Licence, Version 2.0 (the "Licence");
you may not use this file except in compliance
with the Licence. You may obtain a copy of the Licence at
    
http://www.apache.org/licenses/LICENSE-2.0
    
Unless required by applicable law or agreed to in writing,
software distributed under the Licence is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the Licence for the
specific language governing permissions and limitations
under the Licence.
    
Author(s) / Copyright (s): Damon Hart-Davis 2015
*/

import java.io.File;

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

public class Main {
    /**Lock object for touch and rate-limit to serialise operations per process (or class loader).
     * This aims to reduce the chance of a race within the file system.
     * <p>
     * This may also make an in-process cache plausible.
     */
    private static final Object touchLock = new Object();

    /**Touch the specified file, creating if necessary. */
    public static void touch(final File f) throws IOException {
        synchronized (touchLock) {
            if (!f.exists()) {
                new FileOutputStream(f, true).close();
            } else {
                f.setLastModified(System.currentTimeMillis());
            }
        }
    }
}

Related

  1. touch(File file)
  2. touch(File file)
  3. touch(File file)
  4. touch(File file, boolean createIfNeeded)
  5. touch(FileFilter filter, File root, boolean recurse)
  6. touch(final File f)
  7. touch(final File file)
  8. touch(final File file)
  9. touch(final File folder, final String fileName)