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) throws IOException 

Source Link

Document

Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

Usage

From source file:com.zb.app.common.file.FileUtils.java

public static File stream2file(InputStream in) throws IOException {
    final File tempFile = File.createTempFile("stream2file", ".tmp");
    tempFile.deleteOnExit();//from www .j  a  v  a 2 s  .  com

    try (FileOutputStream out = new FileOutputStream(tempFile)) {
        IOUtils.copy(in, out);
    }
    return tempFile;
}

From source file:Main.java

public static File createTempFile(String prefix, String suffix) throws IOException {
    // this allows use to eaily identify all our dtemp files and delete them, since delete on Exit doesn't really work.
    File file = File.createTempFile("ohfu-" + prefix, suffix);
    file.deleteOnExit();// w  ww . ja  v  a2s  . co m
    return file;
}

From source file:Main.java

static File createTempFile(String prefix, String suffix, boolean deleteOnExit) throws IOException {
    final File tempFile = File.createTempFile(prefix, suffix);
    if (deleteOnExit) {
        tempFile.deleteOnExit();//from   w  w  w. j a va  2 s. c  om
    }
    return tempFile;
}

From source file:Main.java

/**
 * Use a temporary file to obtain the name of the default system encoding
 * @return name of default system encoding, or null if write failed
 *///from ww w .j a v  a  2  s .co  m
private static String determineSystemEncoding() {
    File tempFile = null;
    String encoding = null;
    try {
        tempFile = File.createTempFile("gpsprune", null);
        OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(tempFile));
        encoding = getEncoding(writer);
        writer.close();
    } catch (IOException e) {
    } // value stays null
      // Delete temp file
    if (tempFile != null && tempFile.exists()) {
        if (!tempFile.delete()) {
            System.err.println("Cannot delete temp file: " + tempFile.getAbsolutePath());
        }
    }
    // If writing failed (eg permissions) then just ask system for default
    if (encoding == null)
        encoding = Charset.defaultCharset().name();
    return encoding;
}

From source file:com.javacreed.examples.io.RafHelperTest.java

private static File createSampleFile(final String data) throws IOException {
    final File file = File.createTempFile("RafHelper", "test");
    file.deleteOnExit();//from  w w  w .jav a  2s  .  co m
    FileUtils.write(file, data, "UTF-8");

    return file;
}

From source file:Main.java

/**
 * Creates a new temporal XML file and writes the content of the specified object into the XML file. 
 * @param filename            name of the temporal XML file.
 * @param content            Content as an object of the specified class.
 * @param typeParameterClass   Class of the object with the content.
 * @return                  File.//ww w .  j a v a  2 s. c  o m
 */
public static <T> File writeTemp(String filename, T content, Class<T> typeParameterClass) {
    File fileTemp = null;
    try {
        fileTemp = File.createTempFile(filename, XML_EXTENSION);
        fileTemp = write(fileTemp, content, typeParameterClass);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fileTemp;
}

From source file:Main.java

private static File unzip(Context context, InputStream in) throws IOException {
    File tmpDb = null;/* w  ww. java 2 s.  co m*/
    int dbFiles = 0;
    try {
        tmpDb = File.createTempFile("import", ".db");

        ZipInputStream zin = new ZipInputStream(in);
        ZipEntry sourceEntry;
        while (true) {

            sourceEntry = zin.getNextEntry();

            if (sourceEntry == null) {
                break;
            }

            if (sourceEntry.isDirectory()) {
                zin.closeEntry();
                continue;
            }

            FileOutputStream fOut;
            if (sourceEntry.getName().endsWith(".db")) {
                // Write database to tmp file
                fOut = new FileOutputStream(tmpDb);
                dbFiles++;
            } else {
                // Write all other files(images) to files dir in apps data
                int start = sourceEntry.getName().lastIndexOf("/") + 1;
                String name = sourceEntry.getName().substring(start);
                fOut = context.openFileOutput(name, Context.MODE_PRIVATE);
            }

            final OutputStream targetStream = fOut;
            try {
                int read;
                while (true) {
                    byte[] buffer = new byte[1024];
                    read = zin.read(buffer);
                    if (read == -1) {
                        break;
                    }
                    targetStream.write(buffer, 0, read);
                }
                targetStream.flush();
            } finally {
                safeCloseClosable(targetStream);
            }
            zin.closeEntry();
        }
    } finally {
        safeCloseClosable(in);
    }
    if (dbFiles != 1) {
        throw new IllegalStateException("Input file is not a valid backup");
    }
    return tmpDb;
}

From source file:javarestart.Utils.java

static File fetchResourceToTempFile(String resName, String resExt, InputStream from) {
    File temp;/*w  w  w.ja  v a  2 s . com*/
    try {
        temp = File.createTempFile(resName, resExt);
    } catch (IOException e) {
        return null;
    }
    temp.deleteOnExit();
    try (BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(temp))) {
        copy(from, os);
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
    return temp;
}

From source file:it.cnr.ilc.ilcioutils.IlcInputToFile.java

/**
 * Creates a file with the content read from the URL
 * @param theUrl the URL where the content is
 * @return a File with the content from the URL
 *///from   w  w w  .  ja  v a 2s . c  om
public static File createAndWriteTempFileFromUrl(String theUrl) {
    File tempOutputFile = null;
    String message;
    try {
        tempOutputFile = File.createTempFile(TEMP_FILE_PREFIX, TEMP_FILE_SUFFIX);

        FileUtils.copyURLToFile(new URL(theUrl), tempOutputFile);
    } catch (IOException e) {
        message = String.format("Error in accessing URL %s %s", theUrl, e.getMessage());
        Logger.getLogger(CLASS_NAME).log(Level.SEVERE, message);
    }
    return tempOutputFile;

}

From source file:com.amazonaws.services.kinesis.producer.KinesisProducerConfigurationTest.java

private static String writeFile(String contents) {
    try {//w w w .  ja  v  a  2 s . co m
        File f = File.createTempFile(UUID.randomUUID().toString(), "");
        f.deleteOnExit();
        FileUtils.write(f, contents);
        return f.getAbsolutePath();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}