Java Temp File Create nio createTemporaryFileFromURL(final URL url)

Here you can find the source of createTemporaryFileFromURL(final URL url)

Description

Create a temporary file from the contents of a URL.

License

Apache License

Parameter

Parameter Description
url the URL to read from.

Return

the test file.

Declaration

public static File createTemporaryFileFromURL(final URL url) 

Method Source Code

//package com.java2s;
/**/*www  .ja  v a  2  s.c om*/
 * Copyright 2014 VU University Medical Center.
 * Licensed under the Apache License version 2.0 (see http://www.apache.org/licenses/LICENSE-2.0.html).
 */

import java.io.File;

import java.io.IOException;

import java.net.URL;

import java.nio.file.Files;

import java.nio.file.StandardCopyOption;

public class Main {
    /**
     * File name prefix for files that are created.
     */
    private static final String FILE_NAME_PREFIX = "workflow-runner";
    /**
     * File name suffix for files that are created.
     */
    private static final String FILE_NAME_SUFFIX = ".txt";

    /**
     * Create a temporary file from the contents of a URL.
     *
     * @param url the URL to read from.
     * @return the test file.
     */
    public static File createTemporaryFileFromURL(final URL url) {
        File tempFile = null;
        try {
            tempFile = File.createTempFile(FILE_NAME_PREFIX, FILE_NAME_SUFFIX);
            Files.copy(url.openStream(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
        } catch (final IOException e) {
            e.printStackTrace();
        }
        return tempFile;
    }
}

Related

  1. createTempFile(final String name)
  2. createTempFile(String contents)
  3. createTempFile(String name)
  4. createTempFile(String name, String extension, String[] lines)
  5. createTemporaryFile(String extension)