Java File Touch touch(final File file)

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

Description

touch

License

Open Source License

Declaration

public static void touch(final File file) throws IOException 

Method Source Code

//package com.java2s;
//   Licensed under the Apache License, Version 2.0 (the "License"); you may

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;

public class Main {
    public static void touch(final String f) throws IOException {
        touch(new File(f));
    }/* ww  w  . j  a  v  a2 s .co  m*/

    public static void touch(final File file) throws IOException {
        if (file.exists()) {
            if (file.isDirectory()) {
                throw new IOException("File '" + file + "' exists but is a directory");
            }
            if (file.canWrite() == false) {
                throw new IOException("File '" + file + "' cannot be written to");
            }
        } else {
            final File parent = file.getParentFile();
            if (parent != null) {
                if (!parent.mkdirs() && !parent.isDirectory()) {
                    throw new IOException("Directory '" + parent + "' could not be created");
                }
            }
            final OutputStream out = new FileOutputStream(file);
            out.close();
        }
        final boolean success = file.setLastModified(System.currentTimeMillis());
        if (!success) {
            throw new IOException("Unable to set the last modification time for " + file);
        }
    }
}

Related

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