Java Temp File Create nio createTempFile(String contents)

Here you can find the source of createTempFile(String contents)

Description

create Temp File

License

Apache License

Declaration

public static File createTempFile(String contents) throws IOException 

Method Source Code


//package com.java2s;
/*/*from ww w .j a  v  a 2 s .  c om*/
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * 
 *  http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.BufferedOutputStream;
import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import java.nio.charset.StandardCharsets;

import java.util.UUID;

public class Main {
    private static final File TMP = new File(System.getProperty("java.io.tmpdir"),
            "aether-" + UUID.randomUUID().toString().substring(0, 8));

    public static File createTempFile(String contents) throws IOException {
        return createTempFile(contents.getBytes(StandardCharsets.UTF_8), 1);
    }

    public static File createTempFile(byte[] pattern, int repeat) throws IOException {
        mkdirs(TMP);
        File tmpFile = File.createTempFile("tmpfile-", ".data", TMP);
        writeBytes(tmpFile, pattern, repeat);
        return tmpFile;
    }

    public static boolean mkdirs(File directory) {
        if (directory == null) {
            return false;
        }

        if (directory.exists()) {
            return false;
        }
        if (directory.mkdir()) {
            return true;
        }

        File canonDir = null;
        try {
            canonDir = directory.getCanonicalFile();
        } catch (IOException e) {
            return false;
        }

        File parentDir = canonDir.getParentFile();
        return (parentDir != null && (mkdirs(parentDir) || parentDir.exists()) && canonDir.mkdir());
    }

    public static void writeBytes(File file, byte[] pattern, int repeat) throws IOException {
        file.deleteOnExit();
        file.getParentFile().mkdirs();
        OutputStream out = null;
        try {
            out = new BufferedOutputStream(new FileOutputStream(file));
            for (int i = 0; i < repeat; i++) {
                out.write(pattern);
            }
            out.close();
            out = null;
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (final IOException e) {
                // Suppressed due to an exception already thrown in the try block.
            }
        }
    }
}

Related

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