Example usage for org.apache.commons.vfs FileObject moveTo

List of usage examples for org.apache.commons.vfs FileObject moveTo

Introduction

In this page you can find the example usage for org.apache.commons.vfs FileObject moveTo.

Prototype

public void moveTo(FileObject destFile) throws FileSystemException;

Source Link

Document

Move this file.

Usage

From source file:egovframework.rte.fdl.filehandling.EgovFileUtil.java

/**
 * <p>/* w  ww . ja  v a2s .  com*/
 *  ? ?? ?  ??.
 * </p>
 * @param source
 *        <code>String</code>
 * @param target
 *        <code>String</code>
 * @throws Exception
 */
public static void mv(String source, String target) throws Exception {

    try {
        final FileObject src = manager.resolveFile(basefile, source);
        FileObject dest = manager.resolveFile(basefile, target);

        if (dest.exists() && dest.getType() == FileType.FOLDER) {
            dest = dest.resolveFile(src.getName().getBaseName());
        }

        src.moveTo(dest);
    } catch (FileSystemException fse) {
        log.error(fse.toString());
        ;
        throw new FileSystemException(fse);
    }
}

From source file:com.thinkberg.webdav.MoveHandler.java

protected void copyOrMove(FileObject object, FileObject target, int depth) throws FileSystemException {
    object.moveTo(target);
}

From source file:com.thinkberg.moxo.dav.MoveHandler.java

protected void copyOrMove(FileObject object, FileObject target, int depth) throws FileSystemException {
    try {//w w w  .ja va2 s. c o m
        object.moveTo(target);
    } catch (FileSystemException ex) {
        ex.printStackTrace();
        target.copyFrom(object, new DepthFileSelector(depth));
        object.delete(new DepthFileSelector());
    }
}

From source file:com.adito.vfs.VFSOutputStream.java

/**
 * <p>Rename the temporary {@link File} to the original one.</p>
 *///from w  w w  .  ja  va2 s  . c om
protected void rename(FileObject temporary, FileObject original) throws IOException {
    if ((original.exists()) && (!original.delete())) {
        throw new IOException("Unable to delete original file");
    }
    temporary.moveTo(original);
}

From source file:com.newatlanta.appengine.junit.vfs.gae.GaeFolderTestCase.java

public void testFolders() throws Exception {
    // root object
    FileObject rootObject = testRootObject();

    // create folder
    FileObject testFolder = testCreateTestFolder(rootObject);

    // create sub-folders
    FileObject subFolder = testFolder.resolveFile("abc/def/ghi");
    assertFalse(subFolder.exists());//from w w  w  .j a v  a2  s.com
    subFolder.createFolder();
    assertFolder(subFolder);
    assertSubFolders(testFolder, new String[] { "abc/def/ghi", "abc/def", "abc" });
    assertEntity(rootObject);

    // rename
    FileObject srcFolder = testFolder.resolveFile("abc/def");
    FileObject destFolder = testFolder.resolveFile("abc/xyz");
    assertTrue(testFolder.canRenameTo(destFolder));
    srcFolder.moveTo(destFolder);
    assertFolder(destFolder);
    assertFalse(srcFolder.exists());
    assertSubFolders(testFolder, new String[] { "abc/xyz/ghi", "abc/xyz", "abc" });

    // TODO: test moving a folder to a different parent

    // delete
    assertTrue(testFolder.exists());
    testFolder.delete(Selectors.SELECT_ALL);
    assertFalse(testFolder.exists());
}

From source file:net.sf.vfsjfilechooser.plaf.basic.BasicVFSDirectoryModel.java

/**
 * Renames a file in the underlying file system.
 *
 * @param oldFile a <code>File</code> object representing
 *        the existing file/*www  .ja  v  a 2  s  .  c  o  m*/
 * @param newFile a <code>File</code> object representing
 *        the desired new file name
 * @return <code>true</code> if rename succeeded,
 *        otherwise <code>false</code>
 * @since 1.4
 */
public boolean renameFile(FileObject oldFile, FileObject newFile) {
    aLock.writeLock().lock();

    try {
        oldFile.moveTo(newFile);
        validateFileCache();

        return true;
    } catch (Exception e) {
        return false;
    } finally {
        aLock.writeLock().unlock();
    }
}

From source file:com.newatlanta.appengine.vfs.provider.GaeFileObject.java

/**
 * Renames the file. If a folder, recursively rename the children.
 *//* w w w .  j  av  a  2 s . c  om*/
@Override
protected void doRename(FileObject newfile) throws IOException {
    if (this.getType().hasChildren()) { // rename the children
        for (FileObject child : this.getChildren()) {
            String newChildPath = child.getName().getPath().replace(this.getName().getPath(),
                    newfile.getName().getPath());
            child.moveTo(resolveFile(newChildPath));
        }
        newfile.createFolder();
    } else {
        if (this.getContent().isOpen()) { // causes re-attach
            throw new IOException(this.getName() + " content is open");
        }
        GaeFileObject newGaeFile = (GaeFileObject) newfile;
        newGaeFile.metadata.setPropertiesFrom(this.metadata);

        // copy contents (blocks) to new file
        Map<Key, Entity> blocks = datastore.get(getBlockKeys(0));
        List<Entity> newBlocks = new ArrayList<Entity>(blocks.size());
        for (Entity block : blocks.values()) {
            Entity newBlock = newGaeFile.getBlock(block.getKey().getId() - 1);
            newBlock.setPropertiesFrom(block);
            newBlocks.add(newBlock);
        }
        newGaeFile.putContent(newBlocks);
    }
}

From source file:com.thinkberg.vfs.s3.tests.S3FileProviderTest.java

public void testMoveShallowFolder() throws FileSystemException {
    FileObject origFolder = ROOT.resolveFile(FOLDER);
    origFolder.delete(ALL_FILE_SELECTOR);
    origFolder.createFolder();//ww w  . j  a va  2 s . c om

    origFolder.resolveFile("file.0").createFile();
    origFolder.resolveFile("file.1").createFile();
    origFolder.resolveFile("file.2").createFile();

    assertEquals(3, origFolder.getChildren().length);

    FileObject destFolder = ROOT.resolveFile(FOLDER + "_dest");
    destFolder.delete(ALL_FILE_SELECTOR);
    assertFalse(destFolder.exists());

    origFolder.moveTo(destFolder);
    assertFalse(origFolder.exists());
    assertTrue(destFolder.exists());

    assertEquals(3, destFolder.getChildren().length);

    destFolder.delete(ALL_FILE_SELECTOR);

    assertFalse(origFolder.exists());
    assertFalse(destFolder.exists());
}

From source file:com.thinkberg.vfs.s3.tests.S3FileProviderTest.java

public void testMoveDeepFolder() throws FileSystemException {
    FileObject origFolder = ROOT.resolveFile(FOLDER);
    origFolder.delete(ALL_FILE_SELECTOR);
    origFolder.createFolder();/*from  w  w w. j a v a 2 s .  c  o  m*/

    origFolder.resolveFile("file.0").createFile();
    origFolder.resolveFile("file.1").createFile();
    origFolder.resolveFile("file.2").createFile();
    origFolder.resolveFile("subfolder").createFolder();
    origFolder.resolveFile("subfolder").resolveFile("subfile.0").createFile();
    origFolder.resolveFile("subfolder").resolveFile("subfile.1").createFile();
    origFolder.resolveFile("subfolder").resolveFile("subfile.2").createFile();

    FileObject[] origFiles = origFolder.findFiles(ALL_FILE_SELECTOR);
    assertEquals(8, origFiles.length);

    FileObject destFolder = ROOT.resolveFile(FOLDER + "_dest");
    destFolder.delete(ALL_FILE_SELECTOR);
    assertFalse(destFolder.exists());

    origFolder.moveTo(destFolder);
    assertFalse(origFolder.exists());
    assertTrue(destFolder.exists());

    FileObject[] destFiles = destFolder.findFiles(ALL_FILE_SELECTOR);
    assertEquals(8, destFiles.length);

    destFolder.delete(ALL_FILE_SELECTOR);

    assertFalse(origFolder.exists());
    assertFalse(destFolder.exists());
}

From source file:com.panet.imeta.job.entries.movefiles.JobEntryMoveFiles.java

private boolean MoveFile(String shortfilename, FileObject sourcefilename, FileObject destinationfilename,
        FileObject movetofolderfolder, LogWriter log, Job parentJob, Result result) {

    FileObject destinationfile = null;
    boolean retval = false;
    try {//w ww  .ja va  2s.  c  o m
        if (!destinationfilename.exists()) {
            if (!simulate)
                sourcefilename.moveTo(destinationfilename);
            if (log.isDetailed())
                log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved",
                        sourcefilename.getName().toString(), destinationfilename.getName().toString()));

            // add filename to result filename
            if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing"))
                addFileToResultFilenames(destinationfilename.toString(), log, result, parentJob);

            updateSuccess();

        } else {
            if (log.isDetailed())
                log.logDetailed(toString(),
                        Messages.getString("JobMoveFiles.Log.FileExists", destinationfilename.toString()));
            if (iffileexists.equals("overwrite_file")) {
                if (!simulate)
                    sourcefilename.moveTo(destinationfilename);
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileOverwrite",
                            destinationfilename.getName().toString()));

                // add filename to result filename
                if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing"))
                    addFileToResultFilenames(destinationfilename.toString(), log, result, parentJob);

                updateSuccess();

            } else if (iffileexists.equals("unique_name")) {
                String short_filename = shortfilename;

                // return destination short filename
                try {
                    short_filename = getMoveDestinationFilename(short_filename, "ddMMyyyy_HHmmssSSS");
                } catch (Exception e) {
                    log.logError(toString(), Messages.getString(
                            Messages.getString("JobMoveFiles.Error.GettingFilename", short_filename)));
                    return retval;
                }

                String movetofilenamefull = destinationfilename.getParent().toString() + Const.FILE_SEPARATOR
                        + short_filename;
                destinationfile = KettleVFS.getFileObject(movetofilenamefull);

                if (!simulate)
                    sourcefilename.moveTo(destinationfile);
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved",
                            sourcefilename.getName().toString(), destinationfile.getName().toString()));

                // add filename to result filename
                if (add_result_filesname && !iffileexists.equals("fail") && !iffileexists.equals("do_nothing"))
                    addFileToResultFilenames(destinationfile.toString(), log, result, parentJob);

                updateSuccess();
            } else if (iffileexists.equals("delete_file")) {
                if (!simulate)
                    destinationfilename.delete();
                if (log.isDetailed())
                    log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileDeleted",
                            destinationfilename.getName().toString()));
            } else if (iffileexists.equals("move_file")) {
                String short_filename = shortfilename;
                // return destination short filename
                try {
                    short_filename = getMoveDestinationFilename(short_filename, null);
                } catch (Exception e) {
                    log.logError(toString(), Messages.getString(
                            Messages.getString("JobMoveFiles.Error.GettingFilename", short_filename)));
                    return retval;
                }

                String movetofilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR
                        + short_filename;
                destinationfile = KettleVFS.getFileObject(movetofilenamefull);
                if (!destinationfile.exists()) {
                    if (!simulate)
                        sourcefilename.moveTo(destinationfile);
                    if (log.isDetailed())
                        log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved",
                                sourcefilename.getName().toString(), destinationfile.getName().toString()));

                    // add filename to result filename
                    if (add_result_filesname && !iffileexists.equals("fail")
                            && !iffileexists.equals("do_nothing"))
                        addFileToResultFilenames(destinationfile.toString(), log, result, parentJob);

                } else {
                    if (ifmovedfileexists.equals("overwrite_file")) {
                        if (!simulate)
                            sourcefilename.moveTo(destinationfile);
                        if (log.isDetailed())
                            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileOverwrite",
                                    destinationfile.getName().toString()));

                        // add filename to result filename
                        if (add_result_filesname && !iffileexists.equals("fail")
                                && !iffileexists.equals("do_nothing"))
                            addFileToResultFilenames(destinationfile.toString(), log, result, parentJob);

                        updateSuccess();
                    } else if (ifmovedfileexists.equals("unique_name")) {
                        SimpleDateFormat daf = new SimpleDateFormat();
                        Date now = new Date();
                        daf.applyPattern("ddMMyyyy_HHmmssSSS");
                        String dt = daf.format(now);
                        short_filename += "_" + dt;

                        String destinationfilenamefull = movetofolderfolder.toString() + Const.FILE_SEPARATOR
                                + short_filename;
                        destinationfile = KettleVFS.getFileObject(destinationfilenamefull);

                        if (!simulate)
                            sourcefilename.moveTo(destinationfile);
                        if (log.isDetailed())
                            log.logDetailed(toString(), Messages.getString("JobMoveFiles.Log.FileMoved",
                                    destinationfile.getName().toString()));

                        // add filename to result filename
                        if (add_result_filesname && !iffileexists.equals("fail")
                                && !iffileexists.equals("do_nothing"))
                            addFileToResultFilenames(destinationfile.toString(), log, result, parentJob);

                        updateSuccess();
                    } else if (ifmovedfileexists.equals("fail")) {
                        // Update Errors
                        updateErrors();
                    }
                }

            } else if (iffileexists.equals("fail")) {
                // Update Errors
                updateErrors();
            }

        }
    } catch (Exception e) {
        log.logError(toString(), Messages.getString("JobMoveFiles.Error.Exception.MoveProcessError",
                sourcefilename.toString(), destinationfilename.toString(), e.getMessage()));
    } finally {
        if (destinationfile != null) {
            try {
                destinationfile.close();
            } catch (IOException ex) {
            }
            ;
        }
    }
    return retval;
}