Android File Touch touch(File file)

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

Description

Implements the same behavior as the "touch" utility on Unix.

License

Open Source License

Parameter

Parameter Description
file The file to "touch."

Exception

Parameter Description
FileNotFoundException If the file is not found.

Declaration

public static void touch(File file) throws FileNotFoundException 

Method Source Code

//package com.java2s;
/*//from ww  w.  j  a v  a 2 s. c o m
 *   casmi
 *   http://casmi.github.com/
 *   Copyright (C) 2011, Xcoo, Inc.
 *
 *  casmi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;

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

import java.io.OutputStream;

public class Main {
    /**
     * Implements the same behavior as the "touch" utility on Unix.
     * It creates a new file with size 0 byte or, if the file exists already, it
     * is opened and closed without modifying it, but updating the file date and
     * time.
     *
     * @param file The file to "touch."
     *
     * @throws FileNotFoundException
     *             If the file is not found.
     */
    public static void touch(File file) throws FileNotFoundException {

        if (!file.exists()) {
            OutputStream out = new FileOutputStream(file);
            try {
                out.close();
            } catch (IOException e) {
                // Ignore.
            }
        }

        file.setLastModified(System.currentTimeMillis());
    }
}