Example usage for org.apache.commons.io FileUtils deleteQuietly

List of usage examples for org.apache.commons.io FileUtils deleteQuietly

Introduction

In this page you can find the example usage for org.apache.commons.io FileUtils deleteQuietly.

Prototype

public static boolean deleteQuietly(File file) 

Source Link

Document

Deletes a file, never throwing an exception.

Usage

From source file:com.openkm.applet.Util.java

/**
 * Upload scanned document to OpenKM/*w w  w  . j  a  v a  2s.  c o m*/
 * 
 */
public static String createDocument(String token, String path, String fileName, String fileType, String url,
        List<BufferedImage> images) throws MalformedURLException, IOException {
    log.info("createDocument(" + token + ", " + path + ", " + fileName + ", " + fileType + ", " + url + ", "
            + images + ")");
    File tmpDir = createTempDir();
    File tmpFile = new File(tmpDir, fileName + "." + fileType);
    ImageOutputStream ios = ImageIO.createImageOutputStream(tmpFile);
    String response = "";

    try {
        if ("pdf".equals(fileType)) {
            ImageUtils.writePdf(images, ios);
        } else if ("tif".equals(fileType)) {
            ImageUtils.writeTiff(images, ios);
        } else {
            if (!ImageIO.write(images.get(0), fileType, ios)) {
                throw new IOException("Not appropiated writer found!");
            }
        }

        ios.flush();
        ios.close();

        if (token != null) {
            // Send image
            HttpClient client = new DefaultHttpClient();
            MultipartEntity form = new MultipartEntity();
            form.addPart("file", new FileBody(tmpFile));
            form.addPart("path", new StringBody(path, Charset.forName("UTF-8")));
            form.addPart("action", new StringBody("0")); // FancyFileUpload.ACTION_INSERT
            HttpPost post = new HttpPost(url + "/frontend/FileUpload;jsessionid=" + token);
            post.setHeader("Cookie", "jsessionid=" + token);
            post.setEntity(form);
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            response = client.execute(post, responseHandler);
        } else {
            // Store in disk
            String home = System.getProperty("user.home");
            File dst = new File(home, tmpFile.getName());
            copyFile(tmpFile, dst);
            response = "Image copied to " + dst.getPath();
        }
    } finally {
        FileUtils.deleteQuietly(tmpDir);
    }

    log.info("createDocument: " + response);
    return response;
}

From source file:com.splunk.shuttl.archiver.usecases.ThawFunctionalTest.java

@AfterMethod
public void tearDown() {
    FileUtils.deleteQuietly(thawDirectory);
    FileUtils.deleteQuietly(archiverData);
    tearDownLocalConfig(config);
}

From source file:com.dhenton9000.file.FileWatchers.java

/**
 * generate a runnable that will process a file on a thread
 *
 * @param file file to process/* w ww  .  ja v  a 2  s.  c  o  m*/
 * @return the constructed runnable
 */
private Runnable getRunnable(final File file, final IReaderSink sink) {
    return new Runnable() {
        @Override
        public void run() {
            //move from inputDir to tempDir

            File tempFile = new File(tempDir, FilenameUtils.getBaseName(file.getAbsolutePath()) + "_tmp.txt");
            String simpleName = FilenameUtils.getName(tempFile.getAbsolutePath());
            try {
                FileUtils.copyFile(file, tempFile);
                FileUtils.deleteQuietly(file);
            } catch (IOException ex) {
                LOG.error("problem moving to temp " + ex.getMessage());
            }
            LOG.info("$#$move done " + simpleName);
            try {
                LOG.info("$#$processing " + tempFile.getAbsolutePath());

                FileReader in = new FileReader(tempFile);
                BufferedReader bRead = new BufferedReader(in);

                String line = bRead.readLine(); //skipping the header
                //output the line
                while (line != null) {
                    line = bRead.readLine();
                    if (line != null) {
                        LOG.debug(simpleName + " " + line.substring(0, 10) + "...");
                        if (sink != null) {
                            sink.report(line);

                        }
                    }

                }

                // parse each file into individual events
                // 
            } catch (Exception e) {
                LOG.error("$#$tried to process csv " + e.getClass().getName() + " " + e.getMessage());
            }
        }
    };

}

From source file:com.splunk.shuttl.archiver.usecases.ExportCsvFunctionalTest.java

@AfterMethod
public void tearDown() {
    FileUtils.deleteQuietly(bucket.getDirectory());
    FileUtils.deleteQuietly(archiverData);
}

From source file:net.grinder.AgentUpdateHandler.java

public void close() {
    IOUtils.closeQuietly(agentOutputStream);
    FileUtils.deleteQuietly(download);
}

From source file:net.continuumsecurity.runner.StoryRunner.java

private void prepareReportsDir() throws IOException {
    FileUtils.deleteQuietly(new File(LATEST_REPORTS));
    File viewDir = new File(LATEST_REPORTS + File.separator + "view");
    FileUtils.copyDirectory(new File(RESOURCES_DIR), viewDir);
}

From source file:com.ning.metrics.collector.hadoop.processing.LocalSpoolManager.java

public static void cleanupOldSpoolDirectories(final Iterable<File> oldSpoolDirectories) {
    // Cleanup empty directories /_tmp, /_quarantine and /_lock first
    for (final File dir : oldSpoolDirectories) {
        for (final File subDir : dir.listFiles()) {
            if (subDir.isDirectory() && subDir.listFiles().length == 0) {
                log.info(String.format("Deleting empty directory: %s", subDir.toString()));
                FileUtils.deleteQuietly(subDir);
            }//  w  w w  .  ja v  a  2 s .  c  o  m
        }
    }

    for (final File dir : oldSpoolDirectories) {
        if (dir.isDirectory() && dir.listFiles().length == 0) {
            log.info(String.format("Deleting empty directory: %s", dir.toString()));
            FileUtils.deleteQuietly(dir);
        }
    }
}

From source file:com.synopsys.integration.util.CommonZipExpander.java

public void expand(final InputStream sourceArchiveStream, final File targetExpansionDirectory)
        throws IOException, ArchiveException, IntegrationException {
    // it is important to first create the zip file as a stream cannot be
    // unzipped correctly in all cases
    // "If possible, you should always prefer ZipFile over
    // ZipArchiveInputStream."
    // https://commons.apache.org/proper/commons-compress/zip.html#ZipArchiveInputStream_vs_ZipFile
    final File tempZipFile = File.createTempFile("tmpzip", null);
    try {/*from   w  w  w  .j  a  v a2 s . c om*/
        try (FileOutputStream fileOutputStream = new FileOutputStream(tempZipFile)) {
            IOUtils.copy(sourceArchiveStream, fileOutputStream);
        }

        if (!tempZipFile.exists() || tempZipFile.length() <= 0) {
            throw new IntegrationException("The zip file was not created correctly. Please try again.");
        }

        expandUnknownFile(tempZipFile, targetExpansionDirectory);
    } finally {
        FileUtils.deleteQuietly(tempZipFile);
    }
}

From source file:io.github.swagger2markup.GeneralConverterTest.java

@Test
public void testFromStringURIWithoutScheme() throws IOException, URISyntaxException {
    //Given//from w  w w .  ja  va 2  s  .  c  o m
    Path outputDirectory = Paths.get("build/test/asciidoc/pathUri");
    FileUtils.deleteQuietly(outputDirectory.toFile());

    //When
    Swagger2MarkupConverter.from(URI.create("src/test/resources/yaml/swagger_petstore.yaml")).build()
            .toFolder(outputDirectory);

    //Then
    String[] files = outputDirectory.toFile().list();
    assertThat(files).hasSize(4).containsAll(expectedFiles);
}

From source file:edu.usc.goffish.gofs.util.partitioning.metis.MetisPartitioner.java

@Override
public IPartitioning partition(IIdentifiableVertexGraph<? extends IIdentifiableVertex, ? extends IEdge> graph,
        int numPartitions) throws IOException {
    if (graph == null) {
        throw new IllegalArgumentException();
    }/*w  w w  .j  av a2 s  .c om*/
    if (numPartitions < 1) {
        throw new IllegalArgumentException();
    }
    if (graph.isDirected()) {
        throw new IllegalArgumentException();
    }

    Path workingDir = Files.createTempDirectory("gofs_metis");
    try {
        // we assume the graph will always require renumbering
        System.out.print("writing metis input file (with renumbering)... ");
        long time = System.currentTimeMillis();

        // write metis input
        Path metisInputPath = workingDir.resolve(DefaultMetisInput);
        long[] renumbering = MetisGraph.writeAndRenumber(graph, Files.newOutputStream(metisInputPath));

        System.out.println("[" + (System.currentTimeMillis() - time) + "ms]");

        // perform partitioning
        Path metisOutputPath = partition(workingDir, metisInputPath, numPartitions);

        System.out.print("loading metis output... ");
        time = System.currentTimeMillis();

        // read metis output
        IPartitioning partitioning = MetisPartitioning.readAndRenumber(Files.newInputStream(metisOutputPath),
                renumbering);

        System.out.println("[" + (System.currentTimeMillis() - time) + "ms]");

        return partitioning;
    } finally {
        FileUtils.deleteQuietly(workingDir.toFile());
    }
}