Example usage for java.io File createTempFile

List of usage examples for java.io File createTempFile

Introduction

In this page you can find the example usage for java.io File createTempFile.

Prototype

public static File createTempFile(String prefix, String suffix, File directory) throws IOException 

Source Link

Document

Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {

    File f = File.createTempFile("tmp", ".txt", new File("C:/"));

    System.out.println("File path: " + f.getAbsolutePath());

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    File file = null;/*from  w  w  w  . j av a  2  s  . c  om*/
    File dir = new File("C:/");
    file = File.createTempFile("JavaTemp", ".javatemp", dir);

    System.out.println(file.getPath());

}

From source file:Utils.java

/**
 * Creates a new empty temporary directory.
 *///from  w  ww .ja  v  a2 s .  c  om
public static File createTmpDir() throws IOException {
    // create a temporary directory
    File tmpFile = File.createTempFile("tmp", "tmp", new File("."));
    tmpFile.delete();
    tmpFile.mkdir();
    return tmpFile;
}

From source file:Main.java

public static File getTempFile(Context context, String fileName) {
    File file = null;/*from ww  w .j  a  v a 2s .  co m*/
    try {
        file = File.createTempFile(fileName, null, context.getCacheDir());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}

From source file:Main.java

public static File createImageFile() throws IOException {
    // Create an image file name
    File storageDir = Environment.getExternalStorageDirectory();
    File image = File.createTempFile("temp", /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
    );// www .j  av  a  2  s.c om

    return image;
}

From source file:Main.java

public static File createImageFile(String imageFileName) throws IOException {
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(imageFileName, /* prefix */
            ".jpg", /* suffix */
            storageDir /* directory */
    );//ww w  . j a va2 s . co  m
    return image;
}

From source file:Main.java

public static File createTempFile(String prefix, String suffix, File dir, boolean isReCreat)
        throws IOException {
    int exceptionsCount = 0;
    while (true) {
        try {/* w w w  .  j a va 2  s.c o  m*/
            File file = File.createTempFile(prefix, suffix, dir).getCanonicalFile();
            if (isReCreat) {
                file.delete();
                file.createNewFile();
            }
            return file;
        } catch (IOException ioex) { // fixes java.io.WinNTFileSystem.createFileExclusively access denied
            if (++exceptionsCount >= 50) {
                throw ioex;
            }
        }
    }
}

From source file:Main.java

public static File createTemporaryFile(String part, String ext) throws Exception {
    File tempDir = Environment.getExternalStorageDirectory();
    tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
    if (!tempDir.exists()) {
        tempDir.mkdir();//www.ja  v  a2s .  co m
    }
    return File.createTempFile(part, ext, tempDir);
}

From source file:Main.java

public static File createTemporaryFile(String part, String ext) throws Exception {
    File tempDir = Environment.getExternalStorageDirectory();
    tempDir = new File(tempDir.getAbsolutePath() + "/.temp/");
    if (!tempDir.exists()) {
        tempDir.mkdirs();/*from w w  w. ja  va 2 s. c  om*/
    }
    return File.createTempFile(part, ext, tempDir);
}

From source file:Main.java

private static File getTempFile(Context context) {
    File file = null;/*from   ww  w  . ja  va2s  .  c  o m*/
    try {
        String fileName = "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        file = File.createTempFile(fileName, ".jpg", context.getCacheDir());
    } catch (IOException e) {
        e.printStackTrace();
    }
    return file;
}