Java File Touch touch(File file)

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

Description

Touch a file, setting its last modified timestamp to current time

License

Apache License

Parameter

Parameter Description
file a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static void touch(File file) throws IOException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;

import java.io.IOException;

public class Main {
    /**/*from  w  ww.j  ava  2 s  . com*/
     * Touch a file, setting its last modified timestamp to current
     * time
     * 
     * @param file
     * @throws IOException
     */
    public static void touch(File file) throws IOException {
        if (file.exists()) {
            if (!file.setLastModified(System.currentTimeMillis())) {
                throw new IOException("Could not touch file " + file.getAbsolutePath());
            }
        } else {
            try {
                File directory = file.getParentFile();
                if (!directory.exists()) {
                    directory.mkdirs();
                }
                file.createNewFile();
            } catch (IOException e) {
                throw new IOException("Could not create file " + file.getAbsolutePath(), e);
            }
        }
    }
}

Related

  1. touch(File baseDir, String... segments)
  2. touch(File f)
  3. touch(File f)
  4. touch(File file)
  5. touch(File file)
  6. touch(File file)
  7. touch(File file)
  8. touch(File file)