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:org.sejda.core.support.io.OutputWriterHelper.java

/**
 * Moves the input file to the output file
 * //from  ww w  .j a  v  a2  s .com
 * @param input
 *            input file
 * @param output
 *            output file
 * @param existingOutputPolicy
 *            policy to use if an output that already exists is found
 * @throws IOException
 */
private static void moveFile(File input, File output, ExistingOutputPolicy existingOutputPolicy)
        throws IOException {
    if (output.exists()) {
        switch (existingOutputPolicy) {
        case OVERWRITE:
            LOG.debug("Moving {} to {}.", input, output);
            FileUtils.deleteQuietly(output);
            FileUtils.moveFile(input, output);
            break;
        case SKIP:
            LOG.info("Skipping already existing output file {}", output);
            break;
        default:
            throw new IOException(String.format(
                    "Unable to write %s to the already existing file destination %s. (policy is %s)", input,
                    output, existingOutputPolicy));
        }
    } else {
        LOG.debug("Moving {} to {}.", input, output);
        FileUtils.moveFile(input, output);
    }
}

From source file:org.silverpeas.components.silvercrawler.control.SilverCrawlerSessionController.java

public void renameFolder(String folderName, String newName)
        throws SilverCrawlerFolderRenameException, IOException {
    // looks for weird characters in new file name (security)
    if (containsWeirdCharacters(newName)) {
        throw new SilverCrawlerFolderRenameException(getString("silverCrawler.nameIncorrect"));
    }/*from w w w  .  j a va 2s . c om*/

    // Get Full Path
    File after = FileUtils.getFile(getFullPath(newName));
    if (after.exists()) {
        throw new SilverCrawlerFolderRenameException(getString("silverCrawler.folderNameAlreadyExists"));
    }

    // Rename file
    File before = FileUtils.getFile(getFullPath(folderName));
    if (before.isDirectory()) {
        FileUtils.moveDirectory(before, after);
    } else {
        FileUtils.moveFile(before, after);
    }
}

From source file:org.silverpeas.core.contribution.content.form.displayers.WysiwygFCKFieldDisplayer.java

private void moveOrCopyFile(ResourceReference fromPK, ResourceReference toPK, File srcFile, File destFile,
        boolean copy, Map<String, String> oldAndNewFileIds) throws IOException {
    if (copy) {/*www  .  ja va 2s  .  c om*/
        // copy file and change images path (instanceId and imageId) inside
        FileUtils.copyFile(srcFile, destFile);
        changeImagePath(destFile, fromPK.getInstanceId(), toPK.getInstanceId(), oldAndNewFileIds);
    } else {
        // move file and change images path (instanceId only) inside
        FileUtils.moveFile(srcFile, destFile);
        changeInstanceId(destFile, fromPK.getInstanceId(), toPK.getInstanceId());
    }
}

From source file:org.silverpeas.core.process.io.file.FileHandler.java

/**
 * The first given file is not handled. It have to exist in an other path than the one that is
 * handled. The second given file is handled. Both files are handled
 * @see FileUtils/*ww  w.  j a  va2s  .  com*/
 */
public void moveFile(final File srcFile, final HandledFile destFile) throws Exception {
    FileUtils.moveFile(srcFile, getFileForWriting(destFile.getBasePath(), destFile.getFile()));
}

From source file:org.silverpeas.core.process.io.file.FileHandler.java

/**
 * Both of given files are handled//from   w  w  w . j  a v a2s .  co m
 * @see FileUtils
 */
protected void moveFile(final FileBasePath basePath, final File srcFile, final FileBasePath baseDestPath,
        final File destFile) throws Exception {
    verify(basePath, srcFile);
    verify(baseDestPath, destFile);
    FileUtils.moveFile(getFileForWriting(basePath, srcFile, true), getFileForWriting(baseDestPath, destFile));
}

From source file:org.silverpeas.core.util.file.FileUtil.java

/**
 * Moves all files from sub folders to the given root folder.
 * @param rootFolder the root folder from which the sub folders are retrieved and into which the
 * files will be moved if any.//w  w w. j  a v a  2  s.  co m
 * @param deleteFolders true if the sub folders must be deleted.
 * @return an array of {@link File} that represents the found sub folders. The returned array is
 * never null.
 * @throws IOException
 */
public static File[] moveAllFilesAtRootFolder(File rootFolder, boolean deleteFolders) throws IOException {
    File[] foldersAtRoot = rootFolder != null
            ? rootFolder.listFiles((FileFilter) FileFilterUtils.directoryFileFilter())
            : null;
    if (foldersAtRoot != null) {
        for (File folderAtRoot : foldersAtRoot) {
            for (File file : FileUtils.listFiles(folderAtRoot, FileFilterUtils.fileFileFilter(),
                    FileFilterUtils.trueFileFilter())) {
                File newFilePath = new File(rootFolder, file.getName());
                if (!newFilePath.exists()) {
                    FileUtils.moveFile(file, newFilePath);
                }
            }
            if (deleteFolders) {
                FileUtils.deleteQuietly(folderAtRoot);
            }
        }
    }
    return foldersAtRoot != null ? foldersAtRoot : new File[0];
}

From source file:org.silverpeas.process.io.file.FileHandler.java

/**
 * The first given file is not handled. It have to exist in an other path than the one that is
 * handled. The second given file is handled. Both files are handled
 * @see FileUtils//from   w  w w . jav  a  2s. com
 */
public void moveFile(final File srcFile, final HandledFile destFile) throws Exception {
    FileUtils.moveFile(srcFile, destFile.getFile());
}

From source file:org.sonar.home.cache.FileCache.java

private void renameQuietly(File sourceFile, File targetFile) {
    boolean rename = sourceFile.renameTo(targetFile);
    // Check if the file was cached by another process during download
    if (!rename && !targetFile.exists()) {
        log.warn(String.format("Unable to rename %s to %s", sourceFile.getAbsolutePath(),
                targetFile.getAbsolutePath()));
        log.warn(String.format("A copy/delete will be tempted but with no garantee of atomicity"));
        try {//from   w  ww .j  ava  2s . c  om
            FileUtils.moveFile(sourceFile, targetFile);
        } catch (IOException e) {
            throw new IllegalStateException(
                    "Fail to move " + sourceFile.getAbsolutePath() + " to " + targetFile, e);
        }
    }
}

From source file:org.sonar.server.plugins.ServerPluginJarsInstaller.java

private void overridePlugin(File sourceFile, boolean deleteSource) {
    File destDir = fs.getUserPluginsDir();
    File destFile = new File(destDir, sourceFile.getName());
    if (destFile.exists()) {
        // plugin with same filename already installed
        FileUtils.deleteQuietly(destFile);
    }/*from   ww  w  .  j  a  v a  2  s  .  co  m*/

    try {
        if (deleteSource) {
            FileUtils.moveFile(sourceFile, destFile);
        } else {
            FileUtils.copyFile(sourceFile, destFile, true);
        }
    } catch (IOException e) {
        LOG.error(String.format("Fail to move or copy plugin: %s to %s", sourceFile.getAbsolutePath(),
                destFile.getAbsolutePath()), e);
    }

    DefaultPluginMetadata metadata = installer.extractMetadata(destFile, false);
    if (StringUtils.isNotBlank(metadata.getKey())) {
        PluginMetadata existing = pluginByKeys.put(metadata.getKey(), metadata);
        if (existing != null) {
            if (!existing.getFile().getName().equals(destFile.getName())) {
                FileUtils.deleteQuietly(existing.getFile());
            }
            LOG.info("Plugin " + metadata.getKey() + " replaced by new version");
        }
    }
}

From source file:org.sparkcommerce.cms.file.service.StaticAssetStorageServiceImpl.java

protected void createLocalFileFromInputStream(InputStream is, File baseLocalFile) throws IOException {
    FileOutputStream tos = null;/* w ww .  j  a va 2  s. c o  m*/
    FileWorkArea workArea = null;
    try {
        if (!baseLocalFile.getParentFile().exists()) {
            boolean directoriesCreated = false;
            if (!baseLocalFile.getParentFile().exists()) {
                directoriesCreated = baseLocalFile.getParentFile().mkdirs();
                if (!directoriesCreated) {
                    // There is a chance that another VM created the directories.   If not, we may not have 
                    // proper permissions and this is an error we need to report.
                    if (!baseLocalFile.getParentFile().exists()) {
                        throw new RuntimeException("Unable to create middle directories for file: "
                                + baseLocalFile.getAbsolutePath());
                    }
                }
            }
        }

        workArea = sparkFileService.initializeWorkArea();
        File tmpFile = new File(FilenameUtils.concat(workArea.getFilePathLocation(), baseLocalFile.getName()));

        tos = new FileOutputStream(tmpFile);

        IOUtils.copy(is, tos);

        // close the input/output streams before trying to move files around
        is.close();
        tos.close();

        // Adding protection against this file already existing / being written by another thread.
        // Adding locks would be useless here since another VM could be executing the code. 
        if (!baseLocalFile.exists()) {
            try {
                FileUtils.moveFile(tmpFile, baseLocalFile);
            } catch (FileExistsException e) {
                // No problem
                if (LOG.isDebugEnabled()) {
                    LOG.debug("File exists error moving file " + tmpFile.getAbsolutePath(), e);
                }
            }
        }
    } finally {
        IOUtils.closeQuietly(is);
        IOUtils.closeQuietly(tos);

        if (workArea != null) {
            sparkFileService.closeWorkArea(workArea);
        }
    }
}