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:dpfmanager.shell.core.DPFManagerProperties.java

public static Properties getPropertiesConfig() {
    Properties properties = null;
    try {//from  ww  w. j av a  2  s.c o m
        File configFile = new File(getConfigDir() + "/dpfmanager.properties");
        File configFileNew = new File(getDataDir() + "/dpfmanager.properties");
        // Config file
        if (!configFileNew.exists()) {
            // No new file
            if (!configFile.exists()) {
                // No old file, create it
                configFileNew.createNewFile();
            } else {
                // Move old to new
                FileUtils.moveFile(configFile, configFileNew);
            }
        }

        InputStream is = new FileInputStream(configFileNew);
        properties = new Properties();
        properties.load(is);
        is.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    return properties;
}

From source file:com.github.ithildir.liferay.mobile.go.GoSDKBuilder.java

protected void copyResource(String name, String destination) throws IOException {

    File destinationDir = new File(destination);

    destinationDir = new File(destinationDir.getAbsoluteFile(), CharPool.SLASH + name);

    URL sourceURL = getClass().getResource(CharPool.SLASH + name);
    URLConnection sourceConnection = sourceURL.openConnection();

    if (sourceConnection instanceof JarURLConnection) {
        copyJarResource((JarURLConnection) sourceConnection, destinationDir);
    } else {/*from   w w w . j  av a 2s  .  c om*/
        File sourceDir = new File(sourceURL.getPath());

        FileUtils.copyDirectory(sourceDir, destinationDir);
    }

    Iterator<File> itr = FileUtils.iterateFiles(destinationDir, new String[] { "copy" }, true);

    while (itr.hasNext()) {
        File file = itr.next();

        String cleanPath = FilenameUtils.removeExtension(file.getAbsolutePath());

        File cleanFile = new File(cleanPath);

        if (!cleanFile.exists()) {
            FileUtils.moveFile(file, new File(cleanPath));
        } else {
            file.delete();
        }
    }
}

From source file:edu.ucsd.crbs.cws.cluster.SyncWorkflowFileToFileSystemImpl.java

/**
 * Code checks if File for Workflow is on the filesystem.  If not code downloads
 * workflow and writes to file system./* w  w  w  .j  ava 2  s  . c  om*/
 * @param w
 * @throws Exception If any error is encountered
 */
@Override
public void sync(Workflow w) throws Exception {
    //TODO need to implement with this logic:
    //http://stackoverflow.com/questions/8928037/how-do-i-get-to-store-a-downloaded-file-with-java-and-jersey

    //lets handle the easy case
    //check if the workflow file exists if it does we are done
    if (doesWorkflowExistOnFileSystem(w) == true) {
        return;
    }

    File wFile = _fileDownloader.downloadFile(_getURL + "/workflowfile", Constants.WFID_PARAM,
            w.getId().toString(), _userLogin, _token);

    if (wFile == null) {
        throw new Exception("No file obtained from web request to base url: " + _getURL);
    }

    //make the directory for the workflow
    File wfDir = new File(getWorkflowDirectory(w));
    if (wfDir.isDirectory() == false) {
        _log.log(Level.INFO, "Creating directories: {0}", wfDir.getAbsolutePath());
        if (wfDir.mkdirs() == false) {
            throw new Exception("Unable to create directory: " + wfDir.getAbsolutePath());
        }
    }

    FileUtils.moveFile(wFile, getWorkflowFile(w));

    if (doesWorkflowExistOnFileSystem(w) == false) {
        throw new Exception("Unable to put workflow on file system");
    }
}

From source file:com.sitewhere.configuration.ConfigurationMigrationSupport.java

/**
 * Move template file and rename it./*from   w  w w  .  j a v  a 2 s  .  c om*/
 * 
 * @param root
 * @param templateFolder
 * @param templateFilename
 * @throws SiteWhereException
 */
protected static void migrateTemplateFile(File root, File templateFolder, String templateFilename)
        throws SiteWhereException {
    try {
        File templateFile = new File(root, templateFilename);
        FileUtils.moveFileToDirectory(templateFile, templateFolder, false);
        File moved = new File(templateFolder, templateFilename);
        File updated = new File(templateFolder,
                FileSystemTenantConfigurationResolver.DEFAULT_TENANT_CONFIGURATION_FILE + "."
                        + FileSystemTenantConfigurationResolver.TENANT_SUFFIX_ACTIVE);
        if (!moved.getAbsolutePath().equals(updated.getAbsolutePath())) {
            FileUtils.moveFile(moved, updated);
        }
    } catch (IOException e) {
        throw new SiteWhereException("Unable to migrate template file.", e);
    }
}

From source file:com.liferay.mobile.sdk.windows.WindowsSDKBuilder.java

protected void copyResource(String name, String destination) throws IOException {

    File destinationDir = new File(destination);

    destinationDir = destinationDir.getAbsoluteFile();

    URL sourceURL = getClass().getResource(CharPool.SLASH + name);
    URLConnection sourceConnection = sourceURL.openConnection();

    if (sourceConnection instanceof JarURLConnection) {
        copyJarResource((JarURLConnection) sourceConnection, destinationDir);
    } else {/* w  w  w  .  ja  v  a  2 s.co m*/
        File sourceDir = new File(sourceURL.getPath());

        FileUtils.copyDirectory(sourceDir, destinationDir);
    }

    Iterator<File> itr = FileUtils.iterateFiles(destinationDir, new String[] { "copy" }, true);

    while (itr.hasNext()) {
        File file = itr.next();

        String cleanPath = FilenameUtils.removeExtension(file.getAbsolutePath());

        File cleanFile = new File(cleanPath);

        if (!cleanFile.exists()) {
            FileUtils.moveFile(file, new File(cleanPath));
        } else {
            file.delete();
        }
    }
}

From source file:fr.sanofi.fcl4transmart.controllers.listeners.geneExpression.SetStudyTreeListener.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() + ".subject_mapping.tmp");
    File stsmf = ((GeneExpressionData) this.dataType).getStsmf();
    if (stsmf == null) {
        this.setStudyTreeUI.displayMessage("Error: no subject to sample mapping file");
    }/*from  w  w w  .j  a  v  a  2 s  . c o m*/
    String category = "";
    TreeNode node = this.setStudyTreeUI.getRoot();
    if (!node.hasChildren()) {
        this.setStudyTreeUI.displayMessage("You have to set a category code");
        return;
    }
    node = node.getChildren().get(0);
    while (node != null) {
        if (category.compareTo("") == 0) {
            category += node.toString().replace(' ', '_');
        } else {
            category += "+" + node.toString().replace(' ', '_');
        }
        if (node.hasChildren()) {
            node = node.getChildren().get(0);
        } else {
            node = null;
        }
    }
    try {
        FileWriter fw = new FileWriter(file);
        BufferedWriter out = new BufferedWriter(fw);
        out.write(
                "study_id\tsite_id\tsubject_id\tSAMPLE_ID\tPLATFORM\tTISSUETYPE\tATTR1\tATTR2\tcategory_cd\n");

        try {
            BufferedReader br = new BufferedReader(new FileReader(stsmf));
            String line = br.readLine();
            while ((line = br.readLine()) != null) {
                String[] fields = line.split("\t", -1);
                out.write(fields[0] + "\t" + fields[1] + "\t" + fields[2] + "\t" + fields[3] + "\t" + fields[4]
                        + "\t" + fields[5] + "\t" + fields[6] + "\t" + fields[7] + "\t" + category + "\n");
            }
            br.close();
        } catch (Exception e) {
            this.setStudyTreeUI.displayMessage("File error: " + e.getLocalizedMessage());
            out.close();
            e.printStackTrace();
        }
        out.close();
        try {
            File fileDest;
            if (stsmf != null) {
                String fileName = stsmf.getName();
                stsmf.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);
            ((GeneExpressionData) this.dataType).setSTSMF(fileDest);
        } catch (IOException ioe) {
            this.setStudyTreeUI.displayMessage("File error: " + ioe.getLocalizedMessage());
            return;
        }
    } catch (Exception e) {
        this.setStudyTreeUI.displayMessage("Eerror: " + e.getLocalizedMessage());
        e.printStackTrace();
    }
    this.setStudyTreeUI.displayMessage("Subject to sample mapping file updated");
    WorkPart.updateSteps();
    WorkPart.updateFiles();
}

From source file:it.geosolutions.geostore.services.rest.auditing.AuditingFilesManager.java

private void moveOutputFile(File rollingFile) {
    LogUtils.info(logger, "Rolling output file '%s' to '%s'.", outputFile.getPath(), rollingFile.getPath());
    try {//from  www.j  av  a 2s.co  m
        FileUtils.moveFile(outputFile, rollingFile);
    } catch (Exception exception) {
        throw new AuditingException(exception, "Error moving output file '%s' to rolling file '%s'.",
                outputFile.getPath(), rollingFile.getPath());
    }
}

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

@Override
public void handleEvent(Event event) {
    //write in a new file
    File file = new File(this.dataType.getPath().toString() + File.separator
            + this.dataType.getStudy().toString() + ".columns.tmp");
    try {// w w w . j a  va  2  s.  co m
        FileWriter fw = new FileWriter(file);
        BufferedWriter out = new BufferedWriter(fw);
        out.write(
                "Filename\tCategory Code\tColumn Number\tData Label\tData Label Source\tControlled Vocab Code\n");

        //subject identifier
        Vector<File> rawFiles = ((ClinicalData) this.dataType).getRawFiles();
        Vector<String> siteIds = this.setOtherIdsUI.getSiteIds();
        Vector<String> visitNames = this.setOtherIdsUI.getVisitNames();
        for (int i = 0; i < rawFiles.size(); i++) {

            //site identifier
            if (siteIds.elementAt(i).compareTo("") != 0) {
                int columnNumber = FileHandler.getHeaderNumber(rawFiles.elementAt(i), siteIds.elementAt(i));
                if (columnNumber != -1) {
                    out.write(rawFiles.elementAt(i).getName() + "\t\t" + columnNumber + "\tSITE_ID\t\t\n");
                }
            }

            //visit name
            if (visitNames.elementAt(i).compareTo("") != 0) {
                int columnNumber = FileHandler.getHeaderNumber(rawFiles.elementAt(i), visitNames.elementAt(i));
                if (columnNumber != -1) {
                    out.write(rawFiles.elementAt(i).getName() + "\t\t" + columnNumber + "\tVISIT_NAME\t\t\n");
                }
            }
        }
        try {
            BufferedReader br = new BufferedReader(new FileReader(((ClinicalData) this.dataType).getCMF()));
            String line = br.readLine();
            while ((line = br.readLine()) != null) {
                String[] s = line.split("\t", -1);
                if (s[3].compareTo("SITE_ID") != 0 && s[3].compareTo("VISIT_NAME") != 0) {
                    out.write(line + "\n");
                }
            }
            br.close();
        } catch (Exception e) {
            this.setOtherIdsUI.displayMessage("Error: " + e.getLocalizedMessage());
            e.printStackTrace();
            out.close();
        }
        out.close();
        try {
            String fileName = ((ClinicalData) this.dataType).getCMF().getName();
            ((ClinicalData) this.dataType).getCMF().delete();
            File fileDest = new File(this.dataType.getPath() + File.separator + fileName);
            FileUtils.moveFile(file, fileDest);
            ((ClinicalData) this.dataType).setCMF(fileDest);
        } catch (IOException ioe) {
            this.setOtherIdsUI.displayMessage("File error: " + ioe.getLocalizedMessage());
            return;
        }

    } catch (Exception e) {
        this.setOtherIdsUI.displayMessage("Error: " + e.getLocalizedMessage());
        e.printStackTrace();
    }
    this.setOtherIdsUI.displayMessage("Column mapping file updated");
    WorkPart.updateSteps();
    WorkPart.updateFiles();
    UsedFilesPart.sendFilesChanged(dataType);
}

From source file:com.turn.ttorrent.client.storage.FileStorage.java

/** Move the partial file to its final location.
 *//*from   w  w w  .  j  a v  a2 s  .  c o  m*/
@Override
public synchronized void finish() throws IOException {
    this.channel.force(true);

    // Nothing more to do if we're already on the target file.
    if (this.isFinished()) {
        return;
    }

    this.raf.close();
    FileUtils.deleteQuietly(this.target);
    FileUtils.moveFile(this.current, this.target);

    logger.debug("Re-opening torrent byte storage at {}.", this.target.getAbsolutePath());

    this.raf = new RandomAccessFile(this.target, "rw");
    this.raf.setLength(this.size);
    this.channel = this.raf.getChannel();
    this.current = this.target;

    FileUtils.deleteQuietly(this.partial);
    logger.info("Moved torrent data from {} to {}.", this.partial.getName(), this.target.getName());
}

From source file:com.cloudant.sync.datastore.AttachmentManager.java

public void addAttachment(PreparedAttachment a, DocumentRevision rev) throws IOException, SQLException {

    // do it this way to only go thru inputstream once
    // * write to temp location using copyinputstreamtofile
    // * get sha1
    // * stick it into database
    // * move file using sha1 as name

    ContentValues values = new ContentValues();
    long sequence = rev.getSequence();
    String filename = a.attachment.name;
    byte[] sha1 = a.sha1;
    String type = a.attachment.type;
    int encoding = a.attachment.encoding.ordinal();
    long length = a.tempFile.length();
    long revpos = CouchUtils.generationFromRevId(rev.getRevision());

    values.put("sequence", sequence);
    values.put("filename", filename);
    values.put("key", sha1);
    values.put("type", type);
    values.put("encoding", encoding);
    values.put("length", length);
    values.put("encoded_length", length);
    values.put("revpos", revpos);

    // delete and insert in case there is already an attachment at this seq (eg copied over from a previous rev)
    datastore.getSQLDatabase().delete("attachments", " filename = ? and sequence = ? ",
            new String[] { filename, String.valueOf(sequence) });
    long result = datastore.getSQLDatabase().insert("attachments", values);
    if (result == -1) {
        // if we can't insert into DB then don't copy the attachment
        a.tempFile.delete();//from  w w w .j  av  a 2 s  .c  o m
        throw new SQLException("Could not insert attachment " + a + " into database with values " + values
                + "; not copying to attachments directory");
    }
    // move file to blob store, with file name based on sha1
    File newFile = fileFromKey(sha1);
    try {
        FileUtils.moveFile(a.tempFile, newFile);
    } catch (FileExistsException fee) {
        // File with same SHA1 hash in the store, we assume it's the same content so can discard
        // the duplicate data we have just downloaded
        a.tempFile.delete();
    }
}