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(String prefix, String suffix, FileAttribute<?>... attrs) 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.spectralogic.ds3client.helpers.FileObjectPutter_Test.java

/**
 * This test cannot run on Windows without extra privileges
 *//*from w  ww.jav a2s.  c  om*/
@Test
public void testSymlink() throws IOException {
    assumeFalse(Platform.isWindows());
    final Path tempDir = Files.createTempDirectory("ds3_file_object_putter_");
    final Path tempPath = Files.createTempFile(tempDir, "temp_", ".txt");

    try {
        try (final SeekableByteChannel channel = Files.newByteChannel(tempPath, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
            channel.write(ByteBuffer.wrap(testData));
        }

        final Path symLinkPath = tempDir.resolve("sym_" + tempPath.getFileName().toString());
        Files.createSymbolicLink(symLinkPath, tempPath);

        getFileWithPutter(tempDir, symLinkPath);
    } finally {
        Files.deleteIfExists(tempPath);
        Files.deleteIfExists(tempDir);
    }
}

From source file:com.willwinder.universalgcodesender.model.GUIBackendPreprocessorTest.java

@Before
public void setup() throws IOException {
    tempDir = Files.createTempDirectory("tempfiles");
    inputFile = Files.createTempFile(tempDir, "tempfiles", ".tmp");
    outputFile = Files.createTempFile(tempDir, "tempfiles", ".tmp");
}

From source file:mx.eersya.beans.CreateItemBean.java

public void savePicture() {
    try {/*from   www.  ja v  a  2s.  com*/
        Path path = Paths.get("/home/eersya/Projects/W/");
        String filename = FilenameUtils.getBaseName(picture.getFileName());
        String extension = FilenameUtils.getExtension(picture.getFileName());
        Path file = Files.createTempFile(path, filename, "." + extension);
        picturePath = file.toString();
        try (InputStream in = picture.getInputstream()) {
            Files.copy(in, file, StandardCopyOption.REPLACE_EXISTING);
        } catch (IOException exa) {
            System.err.println(exa);
        }
        picturePath = file.toString();
        System.out.println("Picture:" + picturePath);
    } catch (IOException ex) {
        System.err.println(ex);
    }
}

From source file:org.pentaho.di.job.entries.copyfiles.JobEntryCopyFilesIT.java

@Test
public void copyFile() throws Exception {
    Files.createTempFile(source, "file", "");

    Result result = entry.execute(new Result(), 0);
    assertTrue(result.getResult());//from  w  ww .j av a2  s.c om
    assertEquals(0, result.getNrErrors());
}

From source file:org.pentaho.di.job.entries.copyfiles.JobEntryCopyFilesIT.java

@Test
public void copyFileFromSubDirectory() throws Exception {
    entry.setIncludeSubfolders(true);/*from   w  w  w.j  ava 2  s . c  o  m*/

    Path subDirectory = Files.createTempDirectory(source, "sub");
    Files.createTempFile(subDirectory, "file", "");

    Result result = entry.execute(new Result(), 0);
    assertTrue(result.getResult());
    assertEquals(0, result.getNrErrors());
}

From source file:com.dragome.web.helpers.serverside.DragomeCompilerLauncher.java

private static Classpath process(Classpath classPath, DragomeConfigurator configurator) {
    try {//from  w w w  .  j  a v  a  2 s .c  o m
        String path = null;

        String tempDir = System.getProperty("java.io.tmpdir");
        File tmpDir = new File(tempDir + File.separatorChar + "dragomeTemp");
        Path tmpPath = tmpDir.toPath();
        FileUtils.deleteDirectory(tmpDir);
        Files.createDirectories(tmpPath);
        File file = Files.createTempFile(tmpPath, "dragome-merged-", ".jar").toFile();
        file.deleteOnExit();
        path = file.getAbsolutePath();

        try (JarOutputStream jos = new JarOutputStream(new FileOutputStream(file))) {
            List<ClasspathEntry> entries = classPath.getEntries();
            for (ClasspathEntry classpathEntry : entries)
                classpathEntry.copyFilesToJar(jos, new DefaultClasspathFileFilter() {
                    private ArrayList<String> keepClass = new ArrayList<>();

                    public boolean accept(ClasspathFile classpathFile) {
                        boolean result = super.accept(classpathFile);

                        String entryName = classpathFile.getPath();
                        if (!keepClass.contains(entryName)) {
                            keepClass.add(entryName);

                            if (entryName.endsWith(".js") || entryName.endsWith(".class")
                                    || entryName.contains("MANIFEST") || entryName.contains(".html")
                                    || entryName.contains(".css"))
                                result &= true;
                        }
                        return result;
                    }
                });
        }

        if (configurator.isRemoveUnusedCode()) {
            return runProguard(file, configurator);
        } else
            return new Classpath(JarClasspathEntry.createFromPath(path));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:de.elomagic.carafile.server.CloudTest.java

@RunAsClient
@Test/*from   ww w  .  j av  a2  s  . c  o m*/
public void testCloudUpload() throws Exception {
    Runnable r = () -> {
        try {
            cloud.start();
        } catch (Exception ex) {
            cex = ex;
        }
    };
    new Thread(r).start();

    // Create file
    Thread.sleep(2000);
    Path file = Files.createTempFile(basePath, "TESTFILE_", ".txt");

    // Check in the repository folder
    int loop = 10;
    while (loop-- > 0) {
        Thread.sleep(2000);
        Set<CloudFileData> set = cloud.list(Paths.get(""));
        if (set.size() == 1) {
            System.out.println("One item found in the folder. Bingo !!!");
            break;
        }
    }
    if (loop <= 0) {
        Assert.fail("Remote folder not equals one");
    }

    // Delete file
    Files.delete(file);

    // Check in the repository folder
    loop = 10;
    while (loop-- > 0) {
        Thread.sleep(2000);
        Set<CloudFileData> set = cloud.list(Paths.get(""));
        if (set.isEmpty()) {
            System.out.println("No item found in the folder. Bingo !!!");
            break;
        }
    }
    if (loop <= 0) {
        Assert.fail("Remote folder not empty");
    }

    // Check in the repository folder
    if (cex != null) {
        throw cex;
    }

    cloud.stop();
}

From source file:de.hybris.platform.media.storage.impl.MediaCacheRecreatorTest.java

private void createRandomCachedFiles(final int num, final String folderName) throws IOException {
    for (int i = 0; i < num; i++) {
        final String location = RandomStringUtils.randomAlphabetic(10);
        Files.createTempFile(Paths.get(System.getProperty("java.io.tmpdir"), folderName),
                new String(Base64.encode(location.getBytes()))
                        + DefaultLocalMediaFileCacheService.CACHE_FILE_NAME_DELIM,
                ".bin");
    }/*w  ww.j a  va2s .  c  o  m*/
}

From source file:org.schedulesdirect.api.utils.HttpUtils.java

static public Path captureContentToDisk(InputStream ins) {
    Config conf = Config.get();//  ww  w.  j av  a2 s . co  m
    Path p = Paths.get(conf.captureRoot().getAbsolutePath(), "http", "content");
    Path f;

    try {
        Files.createDirectories(p);
        f = Files.createTempFile(p, "sdjson_content_", ".dat");
    } catch (IOException e) {
        LOG.error("Unable to create http content file!", e);
        return null;
    }

    try (OutputStream os = new FileOutputStream(f.toFile())) {
        IOUtils.copy(ins, os);
    } catch (IOException e) {
        LOG.error("Unable to write http content to file!", e);
        return null;
    }

    return f;
}

From source file:com.spectralogic.ds3client.helpers.FileObjectPutter_Test.java

@Test
public void testRegularFile() throws IOException {
    final Path tempDir = Files.createTempDirectory("ds3_file_object_putter_");
    final Path tempPath = Files.createTempFile(tempDir, "temp_", ".txt");

    try {/*  w ww.ja  va 2s  .co  m*/
        try (final SeekableByteChannel channel = Files.newByteChannel(tempPath, StandardOpenOption.CREATE,
                StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
            channel.write(ByteBuffer.wrap(testData));
        }

        getFileWithPutter(tempDir, tempPath);
    } finally {
        Files.deleteIfExists(tempPath);
        Files.deleteIfExists(tempDir);
    }
}