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

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

Introduction

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

Prototype

public static void moveFile(File srcFile, File destFile) throws IOException 

Source Link

Document

Moves a file.

Usage

From source file:net.krotscheck.jersey2.configuration.Jersey2ToolkitConfigTest.java

/**
 * Assert that a missing file does not throw an error.
 *
 * @throws java.io.IOException File operation errors.
 *///w ww .  jav a  2s . c o  m
@Test
public void testMissingFile() throws IOException {
    // Move the properties file out of the way.
    File propFile = ResourceUtil.getFileForResource("jersey2-toolkit.properties");
    File newPropFile = ResourceUtil.getFileForResource("jersey2-toolkit-mv.properties");
    FileUtils.moveFile(propFile, newPropFile);

    Assert.assertFalse(propFile.exists());
    Assert.assertTrue(newPropFile.exists());

    // Add something to check
    System.setProperty("property3", "override3");

    // If this throws an error, we've got a problem.
    Configuration config = new Jersey2ToolkitConfig();
    Assert.assertFalse(config.containsKey("property1"));
    Assert.assertFalse(config.containsKey("property2"));
    Assert.assertTrue(config.containsKey("property3"));

    System.clearProperty("property3");

    // Move the file back.
    FileUtils.moveFile(newPropFile, propFile);

    Assert.assertTrue(propFile.exists());
    Assert.assertFalse(newPropFile.exists());

}

From source file:com.jaeksoft.searchlib.config.ConfigFileRotation.java

public void rotate(VersionFile versionFile) throws IOException {
    lock.rl.lock();/*from  ww  w. j a v a 2 s  . c  o  m*/
    try {
        freeTempPrintWriter();
        if (oldFile.exists())
            oldFile.delete();
        if (!tempFile.exists())
            return;
        if (masterFile.exists())
            FileUtils.moveFile(masterFile, oldFile);
        FileUtils.moveFile(tempFile, masterFile);
        versionFile.increment();
    } finally {
        lock.rl.unlock();
    }
}

From source file:model.RestoredFile.java

/**
 * Moves this file to another location/*from ww  w  .  j av a2 s.c  o  m*/
 * 
 * @param destination
 * @return A copy of this file at another location.
 * @throws IOException
 */
public RestoredFile copy(String destination) throws IOException {
    RestoredFile movedFile = new RestoredFile(destination);
    FileUtils.moveFile(this, movedFile);
    movedFile.wasPayload = this.wasPayload;
    movedFile.wasCarrier = this.wasCarrier;
    movedFile.checksumValid = this.checksumValid;
    movedFile.restorationNote = this.restorationNote;
    movedFile.originalFilePath = this.originalFilePath;
    movedFile.algorithm = this.algorithm;
    movedFile.relatedFiles = this.relatedFiles;
    return movedFile;
}

From source file:net.sf.zekr.engine.audio.AudioCacheManager.java

public void prefetchAudioFileImplicitly(AudioData audioData, int sura, int aya) {
    try {/*from   w w  w .j a v  a  2 s. c o m*/
        String cacheItemName = getCacheItemName(audioData, sura, aya);
        File cachedFile = getCacheItem(cacheItemName);
        if (cachedFile == null || cachedFile.length() <= 0) {
            String fileUrl = String.format(audioData.getOnlineUrl(sura, aya));
            File tmpFile = new File(System.getProperty("java.io.tmpdir"), cacheItemName);
            OutputStream outputStream = new BufferedOutputStream(new FileOutputStream(tmpFile), 1024 * 8);
            IOUtils.copy(net.sf.zekr.common.util.FileUtils.getContent(fileUrl), outputStream);
            FileUtils.moveFile(tmpFile, cachedFile);
        }
    } catch (Exception e) {
        logger.implicitLog(e);
    }
}

From source file:com.zuehlke.sbdfx.dataimport.SourceFilesFetcher.java

private void downloadLink(final WebLink webLink) throws IOException {
    final String urlString = webLink.getURLString();
    OutputStream output = null;//from w  w w. j a v  a  2  s  . c om
    InputStream input = null;
    try {
        if (isLinkToDownload(urlString)) {
            final File targetFile = new File(targetFolder, urlString);
            if (targetFile.exists()) {
                FileUtils.forceDelete(targetFile);
            }
            final File tempFile = new File(targetFolder, "currentDownload");
            if (tempFile.exists()) {
                FileUtils.forceDelete(tempFile);
            }
            LOGGER.info("Downloading {} to {}", urlString, targetFile.getAbsolutePath());
            URL srcUrl = new URL(new URL(SOURCE_FOLDER), urlString);
            input = srcUrl.openStream();
            output = new FileOutputStream(tempFile);
            IOUtils.copyLarge(input, output);
            output.close();
            FileUtils.moveFile(tempFile, targetFile);
        } else {
            LOGGER.info("NOT Downloading {}", urlString);
        }
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(input);
    }
}

From source file:edu.cornell.med.icb.goby.alignments.UpgradeTo1_9_8_2.java

public void upgrade(final String basename, final AlignmentReaderImpl reader) throws IOException {
    if (GobyVersion.isMoreRecent(reader.getGobyVersion(), "goby_1.9.8.2")) {
        return;//from   w  ww  .  j  a  va2  s .  c  o m
    }
    String entriesFilename = basename + ".entries";
    File entriesFile = new File(entriesFilename);
    if (entriesFile.length() < Integer.MAX_VALUE) {
        // entries file smaller than 2GB, nothing to do.
        //System.out.println("Entries file smaller than 2GB, would ignore");
        return;
    }
    System.out.printf("Upgrading %s.. This will take a while.%n", basename);
    ConcatenateAlignmentMode concat = new ConcatenateAlignmentMode();

    String[] inputs = { basename };
    concat.setInputFileNames(inputs);
    String tmpBasename = makeTmpBasename(basename);
    concat.setUpgrade(false);
    concat.setOutputFilename(tmpBasename);
    concat.setIgnoreTmh(true);
    try {
        concat.execute();

        if (new File(tmpBasename + ".index").exists()) {

            FileUtils.moveFile(new File(basename + ".index"),
                    new File(makeBackFilename(basename + ".index", ".bak")));
            FileUtils.moveFile(new File(tmpBasename + ".index"), new File(basename + ".index"));

            upgradeHeaderVersion(basename);
            if (verbose) {
                System.out.printf("alignment %s upgraded successfully.%n", basename);
            }
        }

        else {
            if (verbose) {
                System.out.printf("alignment %s failed to upgrade to 1.9.8.2.%n", basename);
            }
        }
    }

    finally

    {
        FileUtils.deleteQuietly(new File(tmpBasename + ".index"));
        FileUtils.deleteQuietly(new File(tmpBasename + ".entries"));
        FileUtils.deleteQuietly(new File(tmpBasename + ".tmh"));
        FileUtils.deleteQuietly(new File(tmpBasename + ".header"));
        FileUtils.deleteQuietly(new File(tmpBasename + ".stats"));
    }

}

From source file:com.vamonossoftware.core.ArchiveServiceSimpleImpl.java

/**
 * Puts the given file into the archive directory, using a MOVE or COPY based on the constructor parameter 'move'.
 *//*from  w  ww  .  ja  v a  2 s . c om*/
public synchronized File archive(File file) {
    File basePath = new File(archiveDir,
            FastDateFormat.getInstance(pathPattern).format(new DateTime().toDate()));

    int count = 1;
    File path = null;
    do {
        path = new File(basePath, Integer.toString(count));
        count++;
    } while (path.exists());

    path.mkdirs();
    File destFile = new File(path, file.getName());
    try {
        if (move) {
            FileUtils.moveFile(file, destFile);
        } else {
            FileUtils.copyFile(file, destFile);
        }
        return destFile;
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.chinalbs.service.impl.FileServiceImpl.java

private void proccessImage(File tempFile, String sourcePath, String resizedPath, Integer width, Integer height,
        boolean moveSource) {
    String tempPath = System.getProperty("java.io.tmpdir");
    File resizedFile = new File(
            tempPath + File.pathSeparator + "upload_" + UUID.randomUUID() + "." + DEST_EXTENSION);
    ImageUtils.zoom(tempFile, resizedFile, width, height);

    File destFile = new File(resizedPath);
    try {//from  w  w w  .ja  va2  s .  com
        if (moveSource) {
            File destSrcFile = new File(sourcePath);
            FileUtils.moveFile(tempFile, destSrcFile);
        }
        FileUtils.moveFile(resizedFile, destFile);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:info.fetter.logstashforwarder.FileWatcherTest.java

@Test
public void testWildcardWatch() throws InterruptedException, IOException {
    if (System.getProperty("os.name").toLowerCase().contains("win")) {
        logger.warn("Not executing this test on windows");
        return;//from  w w w  .j av a2  s  .  c om
    }
    FileWatcher watcher = new FileWatcher();
    watcher.addFilesToWatch("./testFileWatcher*.txt", new Event().addField("test", "test"),
            FileWatcher.ONE_DAY);
    watcher.initialize();

    File file1 = new File("testFileWatcher1.txt");
    File file2 = new File("testFileWatcher2.txt");
    //File file3 = new File("test3.txt");
    //File file4 = new File("test4.txt");

    //File testDir = new File("testFileWatcher");
    //FileUtils.forceMkdir(new File("test"));

    watcher.checkFiles();
    Thread.sleep(100);
    FileUtils.write(file1, "file 1 line 1\n", true);
    Thread.sleep(100);
    watcher.checkFiles();
    FileUtils.write(file1, "file 1 line 2\n", true);
    //FileUtils.write(file2, "file 2 line 1\n", true);
    Thread.sleep(1000);
    watcher.checkFiles();
    //      FileUtils.moveFileToDirectory(file1, testDir, true);
    //      FileUtils.write(file2, "file 2 line 2\n", true);
    FileUtils.moveFile(file1, file2);
    //      FileUtils.write(file2, "file 3 line 1\n", true);
    //
    Thread.sleep(1000);
    watcher.checkFiles();
    //      
    //      
    watcher.close();
    FileUtils.deleteQuietly(file1);
    FileUtils.deleteQuietly(file2);
    //      FileUtils.forceDelete(testDir);

}

From source file:com.baasbox.db.async.ExportJob.java

@Override
public void run() {
    FileOutputStream dest = null;
    ZipOutputStream zip = null;// ww  w  .  j  ava  2  s .co  m
    FileOutputStream tempJsonOS = null;
    FileInputStream in = null;
    try {
        //File f = new File(this.fileName);
        File f = File.createTempFile("export", ".temp");

        dest = new FileOutputStream(f);
        zip = new ZipOutputStream(dest);

        File tmpJson = File.createTempFile("export", ".json");
        tempJsonOS = new FileOutputStream(tmpJson);
        DbHelper.exportData(this.appcode, tempJsonOS);
        BaasBoxLogger.info(String.format("Writing %d bytes ", tmpJson.length()));
        tempJsonOS.close();

        ZipEntry entry = new ZipEntry("export.json");
        zip.putNextEntry(entry);
        in = new FileInputStream(tmpJson);
        final int BUFFER = BBConfiguration.getImportExportBufferSize();
        byte buffer[] = new byte[BUFFER];

        int length;
        while ((length = in.read(buffer)) > 0) {
            zip.write(buffer, 0, length);
        }
        zip.closeEntry();
        in.close();

        File manifest = File.createTempFile("manifest", ".txt");
        FileUtils.writeStringToFile(manifest,
                BBInternalConstants.IMPORT_MANIFEST_VERSION_PREFIX + BBConfiguration.getApiVersion());

        ZipEntry entryManifest = new ZipEntry("manifest.txt");
        zip.putNextEntry(entryManifest);
        zip.write(FileUtils.readFileToByteArray(manifest));
        zip.closeEntry();

        tmpJson.delete();
        manifest.delete();

        File finaldestination = new File(this.fileName);
        FileUtils.moveFile(f, finaldestination);

    } catch (Exception e) {
        BaasBoxLogger.error(ExceptionUtils.getMessage(e));
    } finally {
        try {
            if (zip != null)
                zip.close();
            if (dest != null)
                dest.close();
            if (tempJsonOS != null)
                tempJsonOS.close();
            if (in != null)
                in.close();

        } catch (Exception ioe) {
            BaasBoxLogger.error(ExceptionUtils.getMessage(ioe));
        }
    }
}