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.ning.arecibo.dao.MysqlTestingHelper.java

public void startMysql() throws IOException {
    dbDir = File.createTempFile("mysql", "");
    dbDir.delete();/* w w w .  j  a  v  a 2 s  .co m*/
    dbDir.mkdir();
    mysqldResource = new MysqldResource(dbDir);

    final Map<String, String> dbOpts = new HashMap<String, String>();
    dbOpts.put(MysqldResourceI.PORT, Integer.toString(port));
    dbOpts.put(MysqldResourceI.INITIALIZE_USER, "true");
    dbOpts.put(MysqldResourceI.INITIALIZE_USER_NAME, USERNAME);
    dbOpts.put(MysqldResourceI.INITIALIZE_PASSWORD, PASSWORD);

    mysqldResource.start("test-mysqld-thread", dbOpts);
    if (!mysqldResource.isRunning()) {
        throw new IllegalStateException("MySQL did not start.");
    } else {
        log.info("MySQL running on port " + mysqldResource.getPort());
        log.info("To connect to it: mysql -u{} -p{} -P{} -S{}/data/mysql.sock {}",
                new Object[] { USERNAME, PASSWORD, port, dbDir, DB_NAME });
    }
}

From source file:edu.cornell.med.icb.learning.libsvm.LibSvmModel.java

@Override
public void write(final OutputStream stream) throws IOException {

    // write the model to a temporary file, then copy the file content to the stream.
    File tmp;//from w w  w .  j a  v a2s.  c o  m
    FileInputStream fis = null;
    try {
        tmp = File.createTempFile("model", ".svm");
        this.write(tmp.getAbsolutePath());

        fis = new FileInputStream(tmp);
        IOUtils.copy(fis, stream);
        tmp.delete();
    } catch (IOException e) {
        throw new RuntimeException("Unable to write model to output stream.", e);
    } finally {
        IOUtils.closeQuietly(fis);
    }
    //throw new UnsupportedOperationException("Unable to write model to a stream in libSVM 3.18");
    //svm.svm_save_model(stream, nativeModel);
}

From source file:org.openmrs.module.moduledistro.web.controller.ModuleDistroManagementController.java

@RequestMapping(value = "/module/moduledistro/manage-upload", method = RequestMethod.POST)
public void handleUpload(@RequestParam("distributionZip") MultipartFile uploaded, HttpServletRequest request,
        Model model) {/*from ww w.ja v a  2  s  . co m*/
    // write this to a known file on disk, so we can use ZipFile, since ZipInputStream is buggy
    File file = null;
    try {
        file = File.createTempFile("distribution", ".zip");
        file.deleteOnExit();
        FileUtils.copyInputStreamToFile(uploaded.getInputStream(), file);
    } catch (Exception ex) {
        throw new RuntimeException("Error getting uploaded data", ex);
    }

    List<String> log = Context.getService(ModuleDistroService.class).uploadDistro(file,
            request.getSession().getServletContext());
    model.addAttribute("log", log);
}

From source file:org.ocpsoft.redoculous.tests.git.InitializeRepositoryTest.java

@Before
public void before() throws IOException, GitAPIException {
    repository = File.createTempFile("redoc", "ulous-test");
    repository.delete();/*from  w  w w.  j a  v a2s .  co m*/
    repository.mkdirs();
    File document = new File(repository, "document.asciidoc");
    document.createNewFile();

    Files.write(document, getClass().getClassLoader().getResourceAsStream("asciidoc/toc.asciidoc"));

    Git repo = Git.init().setDirectory(repository).call();
    repo.add().addFilepattern("document.asciidoc").call();
    repo.commit().setMessage("Initial commit.").call();
}

From source file:com.ksc.auth.profile.ProfilesConfigFileWriterTest.java

@Test
public void testDumpToFile() throws IOException {
    File tmpFile = File.createTempFile("credentials.", null);

    Profile[] abcd = { new Profile("a", basicCredA), new Profile("b", basicCredB),
            new Profile("c", sessionCredC), new Profile("d", sessionCredD) };
    ProfilesConfigFileWriter.dumpToFile(tmpFile, true, abcd);
    checkCredentialsFile(tmpFile, abcd);

    // Rewrite the file with overwrite=true
    Profile[] a = { new Profile("a", basicCredA) };
    ProfilesConfigFileWriter.dumpToFile(tmpFile, true, a);
    checkCredentialsFile(tmpFile, a);/* w  ww  .  j a v a2 s.co  m*/

    // Rewrite the file with overwrite=false is not allowed
    try {
        ProfilesConfigFileWriter.dumpToFile(tmpFile, false, new Profile("a", basicCredA));
        fail("Should have thrown exception since the destination file already exists.");
    } catch (KscClientException expected) {
    }

}

From source file:edu.scripps.fl.pubchem.app.relations.ELinkStage.java

@Override
public void process(Object obj) throws StageException {
    Integer id = (Integer) obj;
    try {/*from   www . j a v a  2s  .  c om*/
        Thread.sleep(333);
        InputStream in = EUtilsWebSession.getInstance()
                .getInputStream("http://www.ncbi.nlm.nih.gov/entrez/eutils/elink.fcgi", "dbfrom", "pcassay",
                        "db", databases, "id", "" + id)
                .call();
        File file = File.createTempFile("pubchem", "link.xml");
        IOUtils.copy(in, new FileOutputStream(file));
        emit(file);
    } catch (Exception ex) {
        throw new StageException(this, ex);
    }
}

From source file:cern.jarrace.controller.io.JarReaderTest.java

@Test(expected = IOException.class)
public void canFailWhenJarDoesNotExist() throws Exception {
    File tmpFile = File.createTempFile("test", null);
    boolean delete = tmpFile.delete();
    if (!delete) {
        throw new IllegalStateException("Failed to delete temporary file " + tmpFile);
    }//  w w  w .j  a v  a 2  s.c o  m
    when(mockedContainer.getContainerPath()).thenReturn(tmpFile.toString());
    JarReader.ofContainer(mockedContainer, Function.identity());
}

From source file:com.opensearchserver.textextractor.ParserAbstract.java

protected final static File createTempFile(InputStream inputStream, String extension) throws IOException {
    File tempFile = File.createTempFile("oss-text-extractor", extension);
    FileOutputStream fos = null;/*from w w  w  .  ja va 2 s  .c  o m*/
    try {
        fos = new FileOutputStream(tempFile);
        IOUtils.copy(inputStream, fos);
        fos.close();
        fos = null;
        return tempFile;
    } finally {
        if (fos != null)
            IOUtils.closeQuietly(fos);
    }
}

From source file:blog.attributes.DetectGenderAgeFromFileExample.java

public static void main(String[] args) throws IOException {
    FaceScenarios faceScenarios = new FaceScenarios(System.getProperty("azure.cognitive.subscriptionKey"),
            System.getProperty("azure.cognitive.emotion.subscriptionKey"));
    File imageFile = File.createTempFile("DetectSingleFaceFromFileExample", "pic");
    //create a new java.io.File from a remote file
    FileUtils.copyURLToFile(new URL(IMAGE_LOCATION), imageFile);
    FileUtils.forceDeleteOnExit(imageFile);
    ImageOverlayBuilder imageOverlayBuilder = ImageOverlayBuilder.builder(imageFile);
    CognitiveJColourPalette colourPalette = CognitiveJColourPalette.STRAWBERRY;
    List<Face> faces = faceScenarios.findFaces(imageFile);
    faces.forEach(face -> imageOverlayBuilder.outlineFaceOnImage(face, RectangleType.FULL,
            ImageOverlayBuilder.DEFAULT_BORDER_WEIGHT, colourPalette)
            .writeAge(face, colourPalette, RectangleTextPosition.TOP_OF));
    imageOverlayBuilder.launchViewer();//from   w ww .j  av a  2s .com

}

From source file:com.htmlhifive.pitalium.common.util.JSONUtilsTest.java

@Test
public void testWriteValueWithIndent() throws Exception {
    file = File.createTempFile("tmp", null);
    JSONUtils.writeValueWithIndent(file, data);

    String result = FileUtils.readFileToString(file);
    assertThat(result.split(System.lineSeparator()), is(new String[] { "{", "  \"a\" : \"b\"", "}" }));
}