Android Directory Temp Create mktempdir()

Here you can find the source of mktempdir()

Description

Makes a unique temporary directory.

License

Apache License

Declaration

public static File mktempdir() throws IOException 

Method Source Code

//package com.java2s;
/**// ww w. j a va  2 s  .c  o m
 * Licensed to Cloudera, Inc. under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  Cloudera, Inc. 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.File;

import java.io.IOException;

public class Main {
    static File baseDir;

    public static File mktempdir(String filePrefix) throws IOException {
        // setup a directory with a bad entry
        File tmpdir = File.createTempFile(filePrefix, ".tmp", baseDir);
        boolean status = tmpdir.delete();
        if (!status) {
            throw new IOException("failed to delete tmp file");
        }
        status = tmpdir.mkdir();
        if (!status) {
            throw new IOException("failed to made tmp dir " + tmpdir);
        }
        return tmpdir;
    }

    /**
     * Makes a unique temporary directory. It is the responsibility of the caller
     * to remove this directory.
     */
    public static File mktempdir() throws IOException {
        return mktempdir("dir");
    }

    /**
     * This wraps the standard create temp file and forces everything into the
     * same baseDir. This makes clean up after testing really trivial.
     * 
     * @param filePrefix
     * @param fileSuffix
     * @return
     * @throws IOException
     */
    public static File createTempFile(String filePrefix, String fileSuffix)
            throws IOException {
        return File.createTempFile(filePrefix, fileSuffix, baseDir);
    }
}

Related

  1. createTempDir()
  2. createTempDir(String dirname)
  3. createTempDir(final String prefix, final String suffix)
  4. createTempDirectory()
  5. mainTempDir()
  6. mktempdir(String filePrefix)
  7. setupTempDir()
  8. tempDir()
  9. getTempDir()