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:au.edu.jcu.fascinator.plugin.harvester.directory.DirectoryNameHarvesterTest.java

@After
public void cleanup() throws Exception {
    try {/*from  w w  w. j  a  va2s  .co  m*/
        Class.forName("org.apache.derby.jdbc.EmbeddedDriver").newInstance();
        DriverManager.getConnection("jdbc:derby:;shutdown=true");
    } catch (SQLException ex) {
        // Expected, derby throws exceptions even during success
    }
    FileUtils.deleteQuietly(testCreatedDir);
    FileUtils.deleteQuietly(cacheDir);
}

From source file:ZipTransformTest.java

public void testByteArrayTransformer() throws IOException {
    final String name = "foo";
    final byte[] contents = "bar".getBytes();

    File file1 = File.createTempFile("temp", null);
    File file2 = File.createTempFile("temp", null);
    try {//from   w w  w.java2s.  c  o  m
        // Create the ZIP file
        ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(file1));
        try {
            zos.putNextEntry(new ZipEntry(name));
            zos.write(contents);
            zos.closeEntry();
        } finally {
            IOUtils.closeQuietly(zos);
        }

        // Transform the ZIP file
        ZipUtil.transformEntry(file1, name, new ByteArrayZipEntryTransformer() {
            protected byte[] transform(ZipEntry zipEntry, byte[] input) throws IOException {
                String s = new String(input);
                assertEquals(new String(contents), s);
                return s.toUpperCase().getBytes();
            }
        }, file2);

        // Test the ZipUtil
        byte[] actual = ZipUtil.unpackEntry(file2, name);
        assertNotNull(actual);
        assertEquals(new String(contents).toUpperCase(), new String(actual));
    } finally {
        FileUtils.deleteQuietly(file1);
        FileUtils.deleteQuietly(file2);
    }
}

From source file:com.samczsun.helios.transformers.compilers.JavaCompiler.java

@Override
public byte[] compile(String name, String contents) {
    File tmpdir = null;//from  w w w.j a  va2 s  . c  om
    File javaFile = null;
    File classFile = null;
    File classpathFile = null;
    try {
        tmpdir = Files.createTempDirectory("javac").toFile();
        javaFile = new File(tmpdir, name + ".java");
        classFile = new File(tmpdir, name + ".class");
        classpathFile = new File(tmpdir, "classpath.jar");
        FileUtils.write(javaFile, contents, "UTF-8", false);
        Utils.save(classpathFile, Helios.getAllLoadedData());

        StringWriter stringWriter = new StringWriter();

        com.sun.tools.javac.main.Main compiler = new com.sun.tools.javac.main.Main("javac",
                new PrintWriter(stringWriter));
        int responseCode = compiler.compile(new String[] { "-d", tmpdir.getAbsolutePath(), "-classpath",
                buildPath(classFile), javaFile.getAbsolutePath() }).exitCode;

        if (responseCode != Main.Result.OK.exitCode) {
            System.out.println(stringWriter.toString());
            Shell shell = SWTUtil.generateLongMessage("Error", stringWriter.toString());
            shell.getDisplay().syncExec(() -> {
                SWTUtil.center(shell);
                shell.open();
            });
        } else {
            return FileUtils.readFileToByteArray(classFile);
        }
    } catch (Exception e) {
        ExceptionHandler.handle(e);
    } finally {
        FileUtils.deleteQuietly(javaFile);
        FileUtils.deleteQuietly(classFile);
        FileUtils.deleteQuietly(classpathFile);
        FileUtils.deleteQuietly(tmpdir);
    }
    return null;
}

From source file:com.splunk.shuttl.prototype.symlink.BucketBlockSymlinkPrototypeTest.java

private void cleanThawDirectory() {
    if (thawLocation != null)
        if (thawLocation.listFiles() != null)
            for (File f : thawLocation.listFiles())
                FileUtils.deleteQuietly(f);
}

From source file:com.mirth.connect.util.messagewriter.MessageWriterArchive.java

/**
 * Ends message writing and moves the written folders/files into the archive file.
 *///www.j a  va  2  s.  c  om
@Override
public void finishWrite() throws MessageWriterException {
    fileWriter.close();

    if (messagesWritten) {
        try {
            File tempFile = new File(
                    archiveFile.getParent() + IOUtils.DIR_SEPARATOR + "." + archiveFile.getName());

            try {
                FileUtils.forceDelete(tempFile);
            } catch (FileNotFoundException e) {
            }

            ArchiveUtils.createArchive(rootFolder, tempFile, archiver, compressor);

            try {
                FileUtils.forceDelete(archiveFile);
            } catch (FileNotFoundException e) {
            }

            FileUtils.moveFile(tempFile, archiveFile);
        } catch (Exception e) {
            throw new MessageWriterException(e);
        } finally {
            FileUtils.deleteQuietly(rootFolder);
        }
    }
}

From source file:com.orange.ocara.model.export.AuditExportService.java

private void exportToDocx(Audit audit) {
    final AssetManager assetManager = getAssets();
    String locale = Locale.getDefault().getLanguage();
    String folder;//from   w ww .j a v a  2 s .  c  o  m
    if (locale.equals("en")) {
        folder = "export/";
    } else {
        folder = "export-" + locale + "/";
    }
    Timber.v("locale " + locale + " folder " + folder);
    final File exportDir = new File(getCacheDir(), folder);

    final File templateDir = new File(exportDir, "docx/template");
    final File workingDir = new File(exportDir, "docx/working");

    final File exportFile = new File(exportDir, String.format("docx/audit_%d.docx", audit.getId()));

    try {
        // Create and cleanup Working and Template directories
        createAndCleanup(workingDir);
        createAndCleanup(templateDir);

        FileUtils.deleteQuietly(exportFile);
        exportFile.createNewFile();

        AssetsHelper.copyAsset(assetManager, folder + "docx", templateDir);
        FileUtils.copyDirectory(templateDir, workingDir);

        DocxExporter auditExporter = new AuditDocxExporter(audit, templateDir);
        DocxWriter writer = DocxWriter.builder().workingDirectory(workingDir).exportFile(exportFile)
                .exporter(auditExporter).build();
        writer.export();

        Intent i = new Intent(EXPORT_SUCCESS);
        i.putExtra("path", exportFile.getPath());
        LocalBroadcastManager.getInstance(this).sendBroadcast(i);

    } catch (Exception e) {
        Timber.e(e, "Failed to copy assets");

        Intent i = new Intent(EXPORT_FAILED);
        LocalBroadcastManager.getInstance(this).sendBroadcast(i);

    } finally {
        FileUtils.deleteQuietly(templateDir);
        FileUtils.deleteQuietly(workingDir);
    }
}

From source file:neembuu.uploader.zip.generator.UpdaterGenerator.java

private void deleteAndMkdirGit() {
    FileUtils.deleteQuietly(gitDirectory);
    gitDirectory.mkdir();
}

From source file:com.splunk.shuttl.archiver.bucketsize.ArchiveBucketsSizeFunctionalTest.java

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

From source file:com.asakusafw.testdriver.compiler.util.DeploymentUtil.java

/**
 * Deletes the target file/directory./*from   ww w  .j ava  2  s. c  o m*/
 * @param target the target file/directory
 * @param options the operation options
 * @return {@code true} if the target file/directory was successfully deleted (or does not exist initially),
 *     otherwise {@code false}
 * @throws IOException if I/O error was occurred
 */
public static boolean delete(File target, DeleteOption... options) throws IOException {
    if (target.exists() == false) {
        return true;
    }
    Set<DeleteOption> opts = EnumSet.noneOf(DeleteOption.class);
    Collections.addAll(opts, options);
    if (opts.contains(DeleteOption.QUIET)) {
        return FileUtils.deleteQuietly(target);
    } else {
        FileUtils.forceDelete(target);
        return true;
    }
}

From source file:com.aliyun.odps.ogg.handler.datahub.BadOperateWriterTest.java

@Test
public void testCheckFileSize() throws IOException {

    for (int i = 0; i < 2; i++) {
        String content = "hello,world";

        if (i == 1) {
            content = "hello,liangyf";
        }/*from   w w  w .ja  v  a  2s  .c om*/

        String fileName = "1.txt";

        File file = new File(fileName);

        FileOutputStream outputStream = FileUtils.openOutputStream(file);

        outputStream.write(content.getBytes());
        outputStream.flush();
        outputStream.close();

        BadOperateWriter.checkFileSize(fileName, 1);

        Assert.assertTrue(!file.exists());

        BufferedReader br = new BufferedReader(new FileReader(fileName + ".bak"));

        String line = "";
        StringBuffer buffer = new StringBuffer();
        while ((line = br.readLine()) != null) {
            buffer.append(line);
        }

        br.close();

        String fileContent = buffer.toString();

        Assert.assertEquals(content, fileContent);
    }

    FileUtils.deleteQuietly(new File("1.txt"));
    FileUtils.deleteQuietly(new File("1.txt.bak"));
}