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:com.netflix.priam.backup.MetaData.java

public static File createTmpMetaFile() throws IOException {
    File metafile = File.createTempFile("meta", ".json");
    File destFile = new File(metafile.getParent(), "meta.json");
    if (destFile.exists())
        destFile.delete();// w  w  w .j ava  2  s .  c o m
    FileUtils.moveFile(metafile, destFile);
    return destFile;
}

From source file:fr.acxio.tools.agia.io.AbstractFileOperations.java

protected void moveFile(Resource sOriginFile, Resource sDestinationFile) throws IOException {
    File aOrigineFile = sOriginFile.getFile();
    if (aOrigineFile.isFile()) {
        if (!isDirectory(sDestinationFile)) {
            FileUtils.moveFile(aOrigineFile, sDestinationFile.getFile());
        } else {/*from  ww w  . ja v  a 2 s.  co  m*/
            FileUtils.moveFileToDirectory(aOrigineFile, sDestinationFile.getFile(), true);
        }
    } else {
        FileUtils.moveDirectoryToDirectory(aOrigineFile, sDestinationFile.getFile(), true);
    }
}

From source file:fr.sanofi.fcl4transmart.controllers.listeners.clinicalData.SetTermsListener.java

@Override
public void handleEvent(Event event) {
    // TODO Auto-generated method stub
    File file = new File(this.dataType.getPath().toString() + File.separator
            + this.dataType.getStudy().toString() + ".words.tmp");
    try {/*w ww .  ja  v  a 2 s . c  o m*/
        FileWriter fw = new FileWriter(file);
        BufferedWriter out = new BufferedWriter(fw);
        out.write("Filename\tColumn Number\tOriginal Data Value\tNew Data Value\n");
        //  
        HashMap<String, Vector<String>> oldValues = this.setTermsUI.getOldValues();
        HashMap<String, Vector<String>> newValues = this.setTermsUI.getNewValues();

        for (String fullName : oldValues.keySet()) {
            File rawFile = new File(this.dataType.getPath() + File.separator + fullName.split(" - ", -1)[0]);
            String header = fullName.split(" - ", -1)[1];
            int columnNumber = FileHandler.getHeaderNumber(rawFile, header);
            for (int i = 0; i < oldValues.get(fullName).size(); i++) {
                if (newValues.get(fullName).elementAt(i).compareTo("") != 0) {
                    out.write(rawFile.getName() + "\t" + columnNumber + "\t"
                            + oldValues.get(fullName).elementAt(i) + "\t" + newValues.get(fullName).elementAt(i)
                            + "\n");
                }
            }
        }

        out.close();
        try {
            File fileDest;
            if (((ClinicalData) this.dataType).getWMF() != null) {
                String fileName = ((ClinicalData) this.dataType).getWMF().getName();
                ((ClinicalData) this.dataType).getWMF().delete();
                fileDest = new File(this.dataType.getPath() + File.separator + fileName);
            } else {
                fileDest = new File(this.dataType.getPath() + File.separator
                        + this.dataType.getStudy().toString() + ".words");
            }
            FileUtils.moveFile(file, fileDest);
            ((ClinicalData) this.dataType).setWMF(fileDest);
        } catch (IOException ioe) {
            this.setTermsUI.displayMessage("File error: " + ioe.getLocalizedMessage());
            return;
        }
    } catch (Exception e) {
        this.setTermsUI.displayMessage("Error: " + e.getLocalizedMessage());
        e.printStackTrace();
    }
    this.setTermsUI.displayMessage("Word mapping file updated");
    WorkPart.updateSteps();
    WorkPart.updateFiles();
    UsedFilesPart.sendFilesChanged(dataType);
}

From source file:de.openflorian.alarm.archive.AlarmFaxArchiver.java

/**
 * Takes given <code>event</code> and moves the parsed and transformed Alarm
 * Fax TIF and TXT files to the//from   w w  w .j a  va  2 s .  com
 * {@link AlarmFaxArchiver#CONFIG_ARCHIVATION_DIRECTORY}
 * 
 * @param event
 */
private void archive(Message<Object> msg) {
    final String toArchive = msg.body().toString();

    if (log.isDebugEnabled())
        log.debug("Archiving file: " + toArchive);

    final File toArchiveFile = new File(toArchive);

    if (toArchiveFile.exists()) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd_HHmmss");
        try {
            final String archiveTarget = String.format("%s%s%s_%s", faxArchivationDirectory, File.separator,
                    format.format(new Date()), toArchiveFile.getName());
            if (log.isDebugEnabled())
                log.debug("Archiving target: " + archiveTarget);

            FileUtils.moveFile(toArchiveFile, new File(archiveTarget));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    } else {
        log.error("File does not exist: " + toArchive);
    }
}

From source file:fr.sanofi.fcl4transmart.controllers.listeners.snpData.SetPlatformListener.java

@Override
public void handleEvent(Event event) {
    Vector<String> values = this.ui.getValues();
    Vector<String> samples = this.ui.getSamples();
    File file = new File(this.dataType.getPath().toString() + File.separator
            + this.dataType.getStudy().toString() + ".subject_mapping.tmp");
    File mappingFile = ((SnpData) this.dataType).getMappingFile();
    if (mappingFile == null) {
        this.ui.displayMessage("Error: no subject to sample mapping file");
    }/*from  ww  w.  j  av a2  s  .  co  m*/
    try {
        FileWriter fw = new FileWriter(file);
        BufferedWriter out = new BufferedWriter(fw);

        try {
            BufferedReader br = new BufferedReader(new FileReader(mappingFile));
            String line;
            while ((line = br.readLine()) != null) {
                String[] fields = line.split("\t", -1);
                String sample = fields[3];
                String platform;
                if (samples.contains(sample)) {
                    platform = values.get(samples.indexOf(sample));
                } else {
                    br.close();
                    return;
                }
                out.write(fields[0] + "\t" + fields[1] + "\t" + fields[2] + "\t" + sample + "\t" + platform
                        + "\t" + fields[5] + "\t" + fields[6] + "\t" + fields[7] + "\t" + fields[8] + "\n");
            }
            br.close();
        } catch (Exception e) {
            this.ui.displayMessage("Error: " + e.getLocalizedMessage());
            out.close();
            e.printStackTrace();
        }
        out.close();
        try {
            File fileDest;
            if (mappingFile != null) {
                String fileName = mappingFile.getName();
                mappingFile.delete();
                fileDest = new File(this.dataType.getPath() + File.separator + fileName);
            } else {
                fileDest = new File(this.dataType.getPath() + File.separator
                        + this.dataType.getStudy().toString() + ".subject_mapping");
            }
            FileUtils.moveFile(file, fileDest);
            ((SnpData) this.dataType).setMappingFile(fileDest);
        } catch (IOException ioe) {
            this.ui.displayMessage("File error: " + ioe.getLocalizedMessage());
            return;
        }
    } catch (Exception e) {
        this.ui.displayMessage("Error: " + e.getLocalizedMessage());
        e.printStackTrace();
    }
    this.ui.displayMessage("Subject to sample mapping file updated");
    WorkPart.updateSteps();
    WorkPart.updateFiles();
}

From source file:com.digitalgeneralists.assurance.model.merge.TargetMergeEngine.java

@Override
public void mergeResult(ComparisonResult result, IProgressMonitor monitor)
        throws AssuranceNullFileReferenceException {
    File sourceFile = result.getSource().getFile();
    File targetFile = result.getTarget().getFile();

    if ((sourceFile == null) || (targetFile == null)) {
        throw new AssuranceNullFileReferenceException("The source or target file is null.");
    }/*from   www . jav a 2  s.c  o m*/

    if (monitor != null) {
        StringBuilder message = new StringBuilder(512);
        monitor.publish(message.append("Merging ").append(targetFile.toString()).append(" to ")
                .append(sourceFile.toString()).toString());
        message.setLength(0);
        message = null;
    }

    if (targetFile.exists()) {
        try {
            if (targetFile.isDirectory()) {
                FileUtils.copyDirectory(targetFile, sourceFile);
            } else {
                FileUtils.copyFile(targetFile, sourceFile);
            }
            result.setResolution(AssuranceResultResolution.REPLACE_SOURCE);
        } catch (IOException e) {
            logger.error("An error occurred when replacing the source with the target.");
            result.setResolution(AssuranceResultResolution.PROCESSING_ERROR_ENCOUNTERED);
            result.setResolutionError(e.getMessage());
        }
    } else {
        if (sourceFile.exists()) {
            File scanDeletedItemsLocation = result
                    .getSourceDeletedItemLocation(getApplicationDeletedItemsLocation());
            if (scanDeletedItemsLocation == null) {
                StringBuilder deletedFilePath = new StringBuilder(512);
                scanDeletedItemsLocation = new File(
                        deletedFilePath.append(this.getDefaultDeletedItemsLocation()).append(File.separator)
                                .append(sourceFile.getName()).toString());
                deletedFilePath.setLength(0);
            }
            try {
                if (sourceFile.isDirectory()) {
                    FileUtils.moveDirectory(sourceFile, scanDeletedItemsLocation);
                } else {
                    FileUtils.moveFile(sourceFile, scanDeletedItemsLocation);
                }
                result.setResolution(AssuranceResultResolution.DELETE_SOURCE);
            } catch (IOException e) {
                StringBuffer message = new StringBuffer(512);
                logger.error(message.append("Could not move item to deleted items location ")
                        .append(sourceFile.getPath()));
                message.setLength(0);
                message = null;
                result.setResolution(AssuranceResultResolution.PROCESSING_ERROR_ENCOUNTERED);
                result.setResolutionError(e.getMessage());
            }
            scanDeletedItemsLocation = null;
        }
    }

    sourceFile = null;
    targetFile = null;
}

From source file:com.digitalgeneralists.assurance.model.merge.SourceMergeEngine.java

@Override
public void mergeResult(ComparisonResult result, IProgressMonitor monitor)
        throws AssuranceNullFileReferenceException {
    File sourceFile = result.getSource().getFile();
    File targetFile = result.getTarget().getFile();

    if ((sourceFile == null) || (targetFile == null)) {
        throw new AssuranceNullFileReferenceException("The source or target file is null.");
    }/*w  w  w  .j av a  2s  .c o  m*/

    if (monitor != null) {
        StringBuilder message = new StringBuilder(512);
        monitor.publish(message.append("Merging ").append(sourceFile.toString()).append(" to ")
                .append(targetFile.toString()).toString());
        message.setLength(0);
        message = null;
    }

    if (sourceFile.exists()) {
        try {
            if (sourceFile.isDirectory()) {
                FileUtils.copyDirectory(sourceFile, targetFile);
            } else {
                FileUtils.copyFile(sourceFile, targetFile);
            }
            result.setResolution(AssuranceResultResolution.REPLACE_TARGET);
        } catch (IOException e) {
            logger.error("An error occurred when replacing the target with the source.");
            result.setResolution(AssuranceResultResolution.PROCESSING_ERROR_ENCOUNTERED);
            result.setResolutionError(e.getMessage());
        }
    } else {
        if (targetFile.exists()) {
            File scanDeletedItemsLocation = result
                    .getTargetDeletedItemLocation(getApplicationDeletedItemsLocation());
            if (scanDeletedItemsLocation == null) {
                StringBuilder deletedItemPath = new StringBuilder(512);
                scanDeletedItemsLocation = new File(
                        deletedItemPath.append(this.getDefaultDeletedItemsLocation()).append(File.separator)
                                .append(targetFile.getName()).toString());
                deletedItemPath.setLength(0);
                deletedItemPath = null;
            }
            try {
                if (targetFile.isDirectory()) {
                    FileUtils.moveDirectory(targetFile, scanDeletedItemsLocation);
                } else {
                    FileUtils.moveFile(targetFile, scanDeletedItemsLocation);
                }
                result.setResolution(AssuranceResultResolution.DELETE_TARGET);
            } catch (IOException e) {
                StringBuffer message = new StringBuffer(512);
                logger.warn(message.append("Could not move item to deleted items location ")
                        .append(sourceFile.getPath()));
                message.setLength(0);
                message = null;
                result.setResolution(AssuranceResultResolution.PROCESSING_ERROR_ENCOUNTERED);
                result.setResolutionError(e.getMessage());
            }
            scanDeletedItemsLocation = null;
        }
    }

    sourceFile = null;
    targetFile = null;
}

From source file:com.digitalgeneralists.assurance.model.merge.MergeEngine.java

public void restoreDeletedItem(ComparisonResult result, IProgressMonitor monitor) {
    File file = null;//from   w  w  w. j a v a  2 s.co  m
    File deletedFile = null;
    if (result.getResolution() == AssuranceResultResolution.DELETE_SOURCE) {
        file = result.getSource().getFile();
        deletedFile = result.getSourceDeletedItemLocation(getApplicationDeletedItemsLocation());
    }
    if (result.getResolution() == AssuranceResultResolution.DELETE_TARGET) {
        file = result.getTarget().getFile();
        deletedFile = result.getTargetDeletedItemLocation(getApplicationDeletedItemsLocation());
    }

    if (file != null) {
        if (monitor != null) {
            StringBuilder message = new StringBuilder(512);
            monitor.publish(message.append("Restoring ").append(file.toString()).toString());
            message.setLength(0);
            message = null;
        }

        if (deletedFile.exists()) {
            try {
                FileUtils.moveFile(deletedFile, file);
                result.setResolution(AssuranceResultResolution.UNRESOLVED);
            } catch (IOException e) {
                StringBuffer message = new StringBuffer(512);
                logger.warn(message.append("Could not move item from deleted items location ")
                        .append(deletedFile.getPath()));
                message.setLength(0);
                message = null;
            }
        } else {
            logger.warn("Item to restore does not exist.");
            result.setResolution(AssuranceResultResolution.PROCESSING_ERROR_ENCOUNTERED);
            result.setResolutionError("Item to restore does not exist.");
        }
    }

    file = null;
    deletedFile = null;
}

From source file:fr.sanofi.fcl4transmart.controllers.listeners.snpData.SetTissueListener.java

@Override
public void handleEvent(Event event) {
    Vector<String> values = this.ui.getValues();
    Vector<String> samples = this.ui.getSamples();
    File file = new File(this.dataType.getPath().toString() + File.separator
            + this.dataType.getStudy().toString() + ".subject_mapping.tmp");
    File mappingFile = ((SnpData) this.dataType).getMappingFile();
    if (mappingFile == null) {
        this.ui.displayMessage("Error: no subject to sample mapping file");
    }/*from  w w w. ja v a 2  s  .  c  o m*/
    try {
        FileWriter fw = new FileWriter(file);
        BufferedWriter out = new BufferedWriter(fw);

        try {
            BufferedReader br = new BufferedReader(new FileReader(mappingFile));
            String line;
            while ((line = br.readLine()) != null) {
                String[] fields = line.split("\t", -1);
                String sample = fields[3];
                String tissueType;
                if (samples.contains(sample)) {
                    tissueType = values.get(samples.indexOf(sample));
                } else {
                    br.close();
                    return;
                }
                out.write(fields[0] + "\t" + fields[1] + "\t" + fields[2] + "\t" + sample + "\t" + fields[4]
                        + "\t" + tissueType + "\t" + fields[6] + "\t" + fields[7] + "\t" + "PLATFORM+TISSUETYPE"
                        + "\n");
            }
            br.close();
        } catch (Exception e) {
            this.ui.displayMessage("File error: " + e.getLocalizedMessage());
            out.close();
            e.printStackTrace();
        }
        out.close();
        try {
            File fileDest;
            if (mappingFile != null) {
                String fileName = mappingFile.getName();
                mappingFile.delete();
                fileDest = new File(this.dataType.getPath() + File.separator + fileName);
            } else {
                fileDest = new File(this.dataType.getPath() + File.separator
                        + this.dataType.getStudy().toString() + ".subject_mapping");
            }
            FileUtils.moveFile(file, fileDest);
            ((SnpData) this.dataType).setMappingFile(fileDest);
        } catch (IOException ioe) {
            this.ui.displayMessage("File error: " + ioe.getLocalizedMessage());
            return;
        }
    } catch (Exception e) {
        this.ui.displayMessage("Error: " + e.getLocalizedMessage());
        e.printStackTrace();
    }
    this.ui.displayMessage("Subject to sample mapping file updated");
    WorkPart.updateSteps();
    WorkPart.updateFiles();
}

From source file:com.twitter.hraven.hadoopJobMonitor.jmx.WhiteList.java

public synchronized String store() throws IOException {
    referesh();/*from  w w w.ja  v a2s  . co m*/
    String filePath = whiteListDir + "/" + WHITELIST_FILENAME;
    String newFilePath = filePath + ".new";
    PrintWriter writer = null;
    try {
        writer = new PrintWriter(newFilePath, "UTF-8");
        Iterator<Map.Entry<String, Date>> iterator = expirationMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<String, Date> entry = iterator.next();
            Date date = entry.getValue();
            writer.println(entry.getKey() + " " + date.getTime() + " " + date);
        }
    } finally {
        if (writer != null)
            writer.close();
    }
    // copy the new file to the standard place
    File file = new File(filePath);
    File newFile = new File(newFilePath);
    file.delete();
    FileUtils.moveFile(newFile, file);
    LOG.warn("WhiteList stored to " + filePath);
    return "Stored in " + filePath;
}