Example usage for java.nio.file Files createTempFile

List of usage examples for java.nio.file Files createTempFile

Introduction

In this page you can find the example usage for java.nio.file Files createTempFile.

Prototype

public static Path createTempFile(Path dir, String prefix, String suffix, FileAttribute<?>... attrs)
        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:org.ulyssis.ipp.publisher.FileOutput.java

@Override
public void outputScore(Score score) {
    Path tmpFile = null;/* w ww  .j  ava2  s.c o m*/
    try {
        if (tmpDir.isPresent()) {
            tmpFile = Files.createTempFile(tmpDir.get(), "score-", ".json",
                    PosixFilePermissions.asFileAttribute(defaultPerms));
        } else {
            tmpFile = Files.createTempFile("score-", ".json",
                    PosixFilePermissions.asFileAttribute(defaultPerms));
        }
        BufferedWriter writer = Files.newBufferedWriter(tmpFile, StandardCharsets.UTF_8);
        Serialization.getJsonMapper().writer(new DefaultPrettyPrinter()).writeValue(writer, score);
        Files.move(tmpFile, filePath, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
    } catch (IOException e) {
        LOG.error("Error writing score to file!", e);
    } finally {
        try {
            if (tmpFile != null)
                Files.deleteIfExists(tmpFile);
        } catch (IOException ignored) {
        }
    }
}

From source file:org.apache.pulsar.io.file.TestFileGenerator.java

private final void createFile() {
    try {/*from  ww w  .j ava  2 s . com*/
        Path path = Files.createTempFile(tempDir, prefix, suffix, attrs);
        try (OutputStream out = Files.newOutputStream(path, StandardOpenOption.APPEND)) {
            for (int idx = 0; idx < numLines; idx++) {
                IOUtils.write(RandomStringUtils.random(50, true, false) + "\n", out, "UTF-8");
            }
        }

        producedFiles.put(path.toFile());

    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:org.nuxeo.runtime.api.Framework.java

/**
 * Creates an empty file in the framework temporary-file directory ({@code nuxeo.tmp.dir} vs {@code java.io.tmpdir}
 * ), using the given prefix and suffix to generate its name. The resulting {@code Path} is associated with the
 * default {@code FileSystem}./*from  w  w w .j  ava 2s. c  o m*/
 * <p>
 * Invoking this method is equivalent to invoking
 * {@link Files#createTempFile(Path, String, String, FileAttribute...)
 * Files.createTempFile(Environment.getDefault().getTemp().toPath(),&nbsp;prefix,&nbsp;suffix,&nbsp;attrs)}.
 *
 * @param prefix the prefix string to be used in generating the file's name; may be {@code null}
 * @param suffix the suffix string to be used in generating the file's name; may be {@code null}, in which case "
 *            {@code .tmp}" is used
 * @param attrs an optional list of file attributes to set atomically when creating the file
 * @return the path to the newly created file that did not exist before this method was invoked
 * @throws IllegalArgumentException if the prefix or suffix parameters cannot be used to generate a candidate file
 *             name
 * @throws UnsupportedOperationException if the array contains an attribute that cannot be set atomically when
 *             creating the directory
 * @throws IOException if an I/O error occurs or the temporary-file directory does not exist
 * @throws SecurityException In the case of the default provider, and a security manager is installed, the
 *             {@link SecurityManager#checkWrite(String) checkWrite} method is invoked to check write access to the
 *             file.
 * @since 8.1
 * @see Files#createTempFile(Path, String, String, FileAttribute...)
 * @see Environment#getTemp()
 * @see #createTempFile(String, String)
 */
public static Path createTempFilePath(String prefix, String suffix, FileAttribute<?>... attrs)
        throws IOException {
    try {
        return Files.createTempFile(getTempDir().toPath(), prefix, suffix, attrs);
    } catch (IOException e) {
        throw new IOException("Could not create temp file in " + getTempDir(), e);
    }
}