Java Temp File Create nio createTempFile(final String name)

Here you can find the source of createTempFile(final String name)

Description

Create temp file TODO ..

License

Open Source License

Parameter

Parameter Description
name file name

Return

file

Declaration

public static File createTempFile(final String name) 

Method Source Code


//package com.java2s;
/*//from  www  .  j  a  va 2 s  .c o m
 * Copyright (C) 2005-2014 Alfresco Software Limited.
 *
 * This file is part of Alfresco
 *
 * Alfresco 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.
 *
 * Alfresco 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 Alfresco. If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;

public class Main {
    /**
     * Create temp file TODO .. support multiple mimetypes .. build files with
     * real size content
     * 
     * @param name file name
     * @return {@link File} file
     */
    public static File createTempFile(final String name) {
        try {
            // create file
            File file = File.createTempFile(name, ".txt");

            // create writer
            OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(file),
                    Charset.forName("UTF-8").newEncoder());
            try {
                // place content in file
                writer.write("this is a sample test upload file");
            } finally {
                // close writer
                writer.close();
            }

            return file;
        } catch (Exception exception) {
            throw new RuntimeException("Unable to create test file.");
        }
    }
}

Related

  1. createTempFile()
  2. createTempFile(final Object forObject)
  3. createTempFile(String contents)
  4. createTempFile(String name)
  5. createTempFile(String name, String extension, String[] lines)
  6. createTemporaryFile(String extension)