Java File Touch touch(final File folder, final String fileName)

Here you can find the source of touch(final File folder, final String fileName)

Description

Creates a file

License

Apache License

Parameter

Parameter Description
folder File
fileName String

Exception

Parameter Description
IOException an exception

Declaration

public static void touch(final File folder, final String fileName) throws IOException 

Method Source Code


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

import java.io.*;

public class Main {
    /**/*from   www.ja  v  a2 s.c o  m*/
     * Creates a file
     *
     * @param folder File
     * @param fileName String
     * @throws IOException
     */
    public static void touch(final File folder, final String fileName) throws IOException {
        if (!folder.exists()) {
            folder.mkdirs();
        }

        final File touchedFile = new File(folder, fileName);

        // The JVM will only 'touch' the file if you instantiate a
        // FileOutputStream instance for the file in question.
        // You don't actually write any data to the file through
        // the FileOutputStream.  Just instantiate it and close it.
        FileOutputStream doneFOS = null;

        try {
            doneFOS = new FileOutputStream(touchedFile);
        } catch (FileNotFoundException e) {
            // Handle error
        } finally {
            if (doneFOS != null) {
                doneFOS.close();
            }
        }
    }
}

Related

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