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

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

Introduction

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

Prototype

public static void moveDirectory(File srcDir, File destDir) throws IOException 

Source Link

Document

Moves a directory.

Usage

From source file:com.blogspot.devsk.l2j.geoconv.GeoGonv.java

public static void main(String[] args) {

    if (args == null || args.length == 0) {
        System.out.println("File name was not specified, [\\d]{1,2}_[\\d]{1,2}.txt will be used");
        args = new String[] { "[\\d]{1,2}_[\\d]{1,2}.txt" };
    }// w  w  w.  j  a va  2s  .  c o m

    File dir = new File(".");
    File[] files = dir.listFiles((FileFilter) new RegexFileFilter(args[0]));

    ArrayList<File> checked = new ArrayList<File>();
    for (File file : files) {
        if (file.isDirectory() || file.isHidden() || !file.exists()) {
            System.out.println(file.getAbsoluteFile() + " was ignored.");
        } else {
            checked.add(file);
        }
    }

    if (OUT_DIR.exists() && OUT_DIR.isDirectory() && OUT_DIR.listFiles().length > 0) {
        try {
            System.out.println("Directory with generated files allready exists, making backup...");
            FileUtils.moveDirectory(OUT_DIR, new File("generated-backup-" + System.currentTimeMillis()));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (!OUT_DIR.exists()) {
        OUT_DIR.mkdir();
    }

    for (File file : checked) {
        GeoConvThreadFactory.startThread(new ParseTask(file));
    }
}

From source file:de.uniwue.dmir.heatmap.EntryPointIncremental.java

@SuppressWarnings({ "rawtypes", "unchecked" })
public static void main(String[] args) throws IOException, ParseException {

    DateFormat df = new SimpleDateFormat(DATE_FORMAT);
    SimpleDateFormat backupDf = new SimpleDateFormat(BACKUP_DATE_FORMAT);

    String workDir = System.getProperty("workDir", ".");
    LOGGER.debug("Work dir: {}", workDir);
    String configDir = System.getProperty("configDir", ".");
    LOGGER.debug("Config dir: {}", configDir);

    File seedDir = new File(workDir, SEED_DIR);
    LOGGER.debug("Seed dir: {}", seedDir);
    File currentDir = new File(workDir, CURRENT_DIR);
    LOGGER.debug("Current dir: {}", currentDir);
    File backupDir = new File(workDir, BACKUP_DIR);
    LOGGER.debug("Backup dir: {}", backupDir);

    String initialMinTimeString = System.getProperty("minTime");
    LOGGER.debug("Initial minimal time parameter: {}", initialMinTimeString);
    Date initialMinTime = initialMinTimeString == null ? new Date(0) : df.parse(initialMinTimeString);
    LOGGER.debug("Initial minimal time: {}", df.format(initialMinTime));

    String absoluteMaxTimeString = System.getProperty("maxTime");
    LOGGER.debug("Absolute maximal time parameter: {}", absoluteMaxTimeString);
    Date absoluteMaxTime = absoluteMaxTimeString == null ? new Date()
            : new SimpleDateFormat(DATE_FORMAT).parse(absoluteMaxTimeString);
    LOGGER.debug("Absolute maximal time: {}", df.format(absoluteMaxTime));

    String incrementalFile = new File("file:" + configDir, INCREMENTAL_FILE).getPath();
    String settingsFile = new File("file:" + configDir, HEATMAP_PROCESSOR__FILE).getPath();

    LOGGER.debug("Initializing incremental control file: {}", incrementalFile);
    FileSystemXmlApplicationContext incrementalContext = new FileSystemXmlApplicationContext(incrementalFile);

    // get point limit
    int pointLimit = Integer
            .parseInt(incrementalContext.getBeanFactory().resolveEmbeddedValue("${point.limit}"));
    LOGGER.debug("Print limit: {}", pointLimit);

    // get backups to keep
    int backupsToKeep = Integer
            .parseInt(incrementalContext.getBeanFactory().resolveEmbeddedValue("${backups.to.keep}"));
    LOGGER.debug("Backups to keep: {}", pointLimit);

    LOGGER.debug("Initializing process components (manager and limiter).");
    IProcessManager processManager = incrementalContext.getBean(IProcessManager.class);
    IProcessLimiter processLimiter = incrementalContext.getBean(IProcessLimiter.class);

    LOGGER.debug("Starting incremental loop.");
    while (true) { // break as soon as no new points are available

        // cleanup --- just in case
        LOGGER.debug("Deleting \"current\" dir.");
        FileUtils.deleteDirectory(currentDir);

        // copy from seed to current
        LOGGER.debug("Copying seed.");
        seedDir.mkdirs();/* w  w w.  jav  a2s.  c o  m*/
        FileUtils.copyDirectory(seedDir, currentDir);

        // get min time
        LOGGER.debug("Getting minimal time ...");
        Date minTime = initialMinTime;
        ProcessManagerEntry entry = processManager.getEntry();
        if (entry != null && entry.getMaxTime() != null) {
            minTime = entry.getMaxTime();
        }
        LOGGER.debug("Minimal time: {}", new SimpleDateFormat(DATE_FORMAT).format(minTime));

        // break if we processed all available points (minTime is greater than or equal to absoluteMaxTime)
        if (minTime.getTime() >= absoluteMaxTime.getTime()) {
            LOGGER.debug("Processed all points.");
            break;
        }

        // get the maximal time
        LOGGER.debug("Get maximal time.");

        // get the time from the newest point in our point range (pointMaxTime) ...
        Date pointMaxTime = processLimiter.getMaxTime(minTime, pointLimit);

        // ... and possibly break the loop if no new points are available
        if (pointMaxTime == null)
            break;

        // set the max time and make sure we are not taking to many points 
        // (set max time to the minimum of pointMaxTime and absoluteMaxTime)
        Date maxTime = pointMaxTime.getTime() > absoluteMaxTime.getTime() ? absoluteMaxTime : pointMaxTime;

        LOGGER.debug("Maximal time: {}", new SimpleDateFormat(DATE_FORMAT).format(maxTime));

        // start process
        processManager.start(minTime);

        System.setProperty("minTimestamp", new SimpleDateFormat(DATE_FORMAT).format(minTime));

        System.setProperty("maxTimestamp", new SimpleDateFormat(DATE_FORMAT).format(maxTime));

        FileSystemXmlApplicationContext heatmapContext = new FileSystemXmlApplicationContext(settingsFile);

        IHeatmap heatmap = heatmapContext.getBean(HEATMAP_BEAN, IHeatmap.class);

        ITileProcessor tileProcessor = heatmapContext.getBean(WRITER_BEAN, ITileProcessor.class);

        heatmap.processTiles(tileProcessor);

        tileProcessor.close();
        heatmapContext.close();

        // finish process
        processManager.finish(maxTime);

        // move old seed
        if (backupsToKeep > 0) {
            FileUtils.moveDirectory(seedDir, new File(backupDir, backupDf.format(minTime))); // minTime is the maxTime of the seed

            // cleanup backups
            String[] backups = backupDir.list(DirectoryFileFilter.DIRECTORY);
            File oldestBackup = null;
            if (backups.length > backupsToKeep) {
                for (String bs : backups) {
                    File b = new File(backupDir, bs);
                    if (oldestBackup == null || oldestBackup.lastModified() > b.lastModified()) {
                        oldestBackup = b;
                    }
                }
                FileUtils.deleteDirectory(oldestBackup);
            }

        } else {
            FileUtils.deleteDirectory(seedDir);
        }

        // move new seed
        FileUtils.moveDirectory(currentDir, seedDir);

    }

    incrementalContext.close();

}

From source file:com.liferay.maven.plugins.util.FileUtil.java

public static boolean move(File source, File destination) {
    if (!source.exists()) {
        return false;
    }//from  w w  w .  ja  v a 2 s  . com

    destination.delete();

    try {
        if (source.isDirectory()) {
            FileUtils.moveDirectory(source, destination);
        } else {
            FileUtils.moveFile(source, destination);
        }
    } catch (IOException ioe) {
        return false;
    }

    return true;
}

From source file:com.jwrapper.maven.seven.SevenMoveFolderMojo.java

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
    try {// ww  w .j a v  a 2 s.co  m

        logger().info("Move source: {}", sevenMoveSource());
        logger().info("Move target: {}", sevenMoveTarget());

        FileUtils.moveDirectory(new File(sevenMoveSource()), new File(sevenMoveTarget()));

    } catch (final Throwable e) {
        logger().error("", e);
        throw new MojoExecutionException("", e);
    }
}

From source file:com.sangupta.shire.site.SiteBackup.java

/**
 * Method to restore the _site.backup folder that we took in this very session.
 *//* www .  j  a v a2s  . c o m*/
private void restoreSiteBackup() {
    if (backupFolder != null) {
        // build up the name of the original folder
        String path = backupFolder.getAbsolutePath();
        path = StringUtils.left(path, path.length() - BACKUP_FOLDER_EXTENSION.length());
        File original = new File(path);

        // delete the currently made site folder
        if (original.exists()) {
            FileUtils.deleteQuietly(original);
        }

        // restore the backup
        try {
            FileUtils.moveDirectory(backupFolder, original);
        } catch (IOException e) {
            System.out.println("Unable to restore the original site backup.");
            e.printStackTrace();
        }
    }
}

From source file:de.fatalix.book.importer.BookMigrator.java

public static void filterBooks(List<File> bookFolders, File filteredFolder) throws IOException {
    int total = bookFolders.size();
    int counter = 0;
    int filteredCounter = 0;
    int percentageDone = 0;
    Gson gson = new Gson();
    File coverFilterFolder = new File(filteredFolder, "cover");
    File descriptionFilterFolder = new File(filteredFolder, "description");
    for (File bookFolder : bookFolders) {
        BookEntry bookEntry = importBatchWise(bookFolder, gson);
        boolean filtered = false;
        if (bookEntry.getCover() == null) {
            filteredCounter++;/*ww  w .j a  v a  2 s.c  om*/
            try {
                String validFolderName = toValidFileName(bookEntry.getAuthor() + "-" + bookEntry.getTitle());
                FileUtils.moveDirectory(bookFolder, new File(coverFilterFolder, validFolderName));
            } catch (IOException ex) {
                System.out.println("Catched...");
            }
            System.out.println("Filtered " + filteredCounter + " of " + total);
            filtered = true;
        }

        if (!filtered && bookEntry.getDescription() == null) {
            filteredCounter++;
            try {
                String validFolderName = toValidFileName(bookEntry.getAuthor() + "-" + bookEntry.getTitle());
                FileUtils.moveDirectory(bookFolder, new File(descriptionFilterFolder, validFolderName));
            } catch (IOException ex) {
                System.out.println("Catched...");
            }
            System.out.println("Filtered " + filteredCounter + " of " + total);
            filtered = true;
        }

        counter++;

        int currentProgress = counter * 100 / total;
        if (currentProgress > percentageDone) {
            percentageDone = currentProgress;
            System.out.println(percentageDone + "% done..");
        }

    }
    System.out.println("Finished processing");
}

From source file:edu.monash.merc.util.file.DMFileUtils.java

public static void moveDirectory(String olderDirName, String newDirName) {
    if (olderDirName == null) {
        throw new DMFileException("old directory name must not be null");
    }//  w w w  .ja v  a2s .  c om
    if (newDirName == null) {
        throw new DMFileException("new directory name must not be null");
    }
    try {
        FileUtils.moveDirectory(new File(olderDirName), new File(newDirName));
    } catch (Exception e) {
        throw new DMFileException(e);
    }
}

From source file:ie.programmer.catcher.browser.AsyncTasks.AsyncMoveTask.java

@Override
protected Boolean doInBackground(String... params) {
    if (mMoveInterface != null)
        mMoveInterface.preMoveStartAsync();
    if (mSourceFile == null || mDestDirFile == null) {
        if (mMoveInterface != null)
            mMoveInterface.onMoveCompleteAsync(false);
        return false;
    }// w  w w  .j a  v  a  2 s.  co m
    if (mSourceFile.isDirectory()) {
        try {
            FileUtils.moveDirectory(mSourceFile, mDestDirFile);
        } catch (Exception e) {
            if (mMoveInterface != null)
                mMoveInterface.onMoveCompleteAsync(false);
            return false;
        }
    } else {
        try {
            FileUtils.moveFile(mSourceFile, mDestDirFile);
        } catch (Exception e) {
            if (mMoveInterface != null)
                mMoveInterface.onMoveCompleteAsync(false);
            return false;
        }
    }
    if (mMoveInterface != null)
        mMoveInterface.onMoveCompleteAsync(true);
    return true;
}

From source file:net.shopxx.service.impl.ThemeServiceImpl.java

public boolean upload(MultipartFile multipartFile) {
    if (multipartFile == null || multipartFile.isEmpty()) {
        return false;
    }/*  ww  w. j a  v  a2  s .com*/
    File tempThemeFile = new File(FileUtils.getTempDirectory(), UUID.randomUUID() + ".tmp");
    File tempThemeDir = new File(FileUtils.getTempDirectory(), UUID.randomUUID().toString());
    try {
        multipartFile.transferTo(tempThemeFile);
        CompressUtils.extract(tempThemeFile, tempThemeDir);
        File themeXmlFile = new File(tempThemeDir, "/template/theme.xml");
        if (themeXmlFile.exists() && themeXmlFile.isFile()) {
            Theme theme = get(themeXmlFile);
            if (theme != null && StringUtils.isNotEmpty(theme.getId())) {
                FileUtils.moveDirectory(new File(tempThemeDir, "/template"),
                        new File(servletContext.getRealPath(themeTemplatePath), theme.getId()));
                FileUtils.moveDirectory(new File(tempThemeDir, "/resources"),
                        new File(servletContext.getRealPath(themeResourcePath), theme.getId()));
                return true;
            }
        }
    } catch (IllegalStateException e) {
        throw new RuntimeException(e.getMessage(), e);
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    } finally {
        FileUtils.deleteQuietly(tempThemeFile);
        FileUtils.deleteQuietly(tempThemeDir);
    }
    return false;
}

From source file:com.psaravan.filebrowserview.lib.AsyncTasks.AsyncMoveTask.java

@Override
protected Boolean doInBackground(String... params) {

    if (mMoveInterface != null)
        mMoveInterface.preMoveStartAsync();

    if (mSourceFile == null || mDestDirFile == null) {
        if (mMoveInterface != null)
            mMoveInterface.onMoveCompleteAsync(false);

        return false;
    }//from   w ww  .j  ava  2 s . c  o  m

    if (mSourceFile.isDirectory()) {
        try {
            FileUtils.moveDirectory(mSourceFile, mDestDirFile);
        } catch (Exception e) {
            if (mMoveInterface != null)
                mMoveInterface.onMoveCompleteAsync(false);

            return false;
        }

    } else {
        try {
            FileUtils.moveFile(mSourceFile, mDestDirFile);
        } catch (Exception e) {
            if (mMoveInterface != null)
                mMoveInterface.onMoveCompleteAsync(false);

            return false;
        }

    }

    if (mMoveInterface != null)
        mMoveInterface.onMoveCompleteAsync(true);

    return true;
}