Example usage for java.io File renameTo

List of usage examples for java.io File renameTo

Introduction

In this page you can find the example usage for java.io File renameTo.

Prototype

public boolean renameTo(File dest) 

Source Link

Document

Renames the file denoted by this abstract pathname.

Usage

From source file:com.joshlong.esb.springintegration.modules.net.sftp.SFTPInboundSynchronizer.java

@SuppressWarnings("ignored")
private boolean copyFromRemoteToLocalDirectory(SFTPSession sftpSession, ChannelSftp.LsEntry entry,
        Resource localDir) throws Exception {
    logger.debug(String.format("attempting to sync remote file %s/%s to local file %s", remotePath,
            entry.getFilename(), localDir.getFile().getAbsolutePath()));

    File fileForLocalDir = localDir.getFile();

    File localFile = new File(fileForLocalDir, entry.getFilename());

    if (!localFile.exists()) {
        InputStream in = null;//from  ww  w  . jav a 2  s.  c  o m
        FileOutputStream fos = null;

        try {
            File tmpLocalTarget = new File(localFile.getAbsolutePath() + INCOMPLETE_EXTENSION);

            fos = new FileOutputStream(tmpLocalTarget);

            String remoteFqPath = this.remotePath + "/" + entry.getFilename();
            in = sftpSession.getChannel().get(remoteFqPath);
            IOUtils.copy(in, fos);

            if (tmpLocalTarget.renameTo(localFile)) {
                // last step
                if (isShouldDeleteDownloadedRemoteFiles()) {
                    logger.debug(String.format("isShouldDeleteDownloadedRemoteFiles == true; "
                            + "attempting to remove remote path '%s'", remoteFqPath));
                    sftpSession.getChannel().rm(remoteFqPath);
                }
            }

            return true;
        } catch (Throwable th) {
            IOUtils.closeQuietly(in);
            IOUtils.closeQuietly(fos);
        }
    } else {
        logger.debug(String.format("local file %s already exists. Not re-downloading it.",
                localFile.getAbsolutePath()));

        return true;
    }

    return false;
}

From source file:com.taobao.android.builder.tasks.library.JarExtractTask.java

/**
 * AWB/*from  www  .  j  a v a  2s. c  o m*/
 */
public void generateJarArtifict(final Zip bundleTask) {

    bundleTask.doLast(new Action<Task>() {

        @Override
        public void execute(Task task) {

            File outputFile = new File(bundleTask.getDestinationDir(), bundleTask.getArchiveName());

            if (!outputFile.exists()) {
                return;
            }

            File f = ZipUtils.extractZipFileToFolder(outputFile, "classes.jar", outputFile.getParentFile());

            if (null != f && f.exists()) {
                File jar = new File(new File(bundleTask.getDestinationDir().getParentFile(), "jar"),
                        FilenameUtils.getBaseName(bundleTask.getArchiveName()) + ".jar");
                jar.getParentFile().mkdirs();
                f.renameTo(jar);

            }

        }
    });
}

From source file:com.fanniemae.ezpie.actions.Directory.java

protected String renameDirectory() throws IOException {
    File fi = new File(_path);
    if (!fi.exists()) {
        throw new PieException(String.format("Directory %s not found, nothing to rename.", _path));
    } else if (fi.isDirectory()) {
        _session.addLogMessage("", "Process", String.format("Renaming %s to %s.", _path, _newName));
        fi.renameTo(new File(_newName));
        _session.addLogMessage("", "", "Completed");
    } else if (fi.isFile()) {
        throw new PieException(
                String.format("%s is a file.  Use the File.Rename operations to work with files.", _path));
    }/*from  w  ww .  j  a v a 2 s.  c  o m*/
    return "";
}

From source file:jetbrains.buildServer.vmgr.agent.Utils.java

public String loadJSONFromFile(String buildID, String workPlacePath, String vInputFile,
        BuildProgressLogger logger, boolean deleteInputFile) throws Exception {
    String output = null;//  w  w  w .j a va2 s .  c  om
    StringBuffer listOfNames = new StringBuffer();
    BufferedReader reader = null;
    String fileName = null;
    boolean notInTestMode = true;
    if (logger == null) {
        notInTestMode = false;
    }

    // Set the right File name.
    if ("".equals(vInputFile) || vInputFile == null) {
        fileName = workPlacePath + File.separator + buildID + File.separator + "vapi.input";
    } else {
        fileName = vInputFile;
    }

    try {

        reader = this.loadFileFromWorkSpace(buildID, workPlacePath, vInputFile, logger, deleteInputFile,
                "vapi.input");
        String line = null;
        while ((line = reader.readLine()) != null) {
            listOfNames.append(line);
        }

    } catch (Exception e) {

        if (notInTestMode) {
            logger.message("Failed to read json input file for the vAPI input.  Failed to load file '"
                    + fileName + "'");
        } else {

            System.out.println(
                    "Failed to open the read file for the vAPI input.  Failed to load file '" + fileName + "'");
        }

        throw e;
    } finally {
        reader.close();
    }

    output = listOfNames.toString();

    if (notInTestMode) {
        logger.message("Input jSON for vAPI is:");
        logger.message(output);
    }

    if (deleteInputFile) {
        if (notInTestMode) {
            logger.message("Job set to delete the input file.  Deleting " + fileName);
        }
        try {
            File fileToDelete = new File(fileName);
            fileToDelete.renameTo(new File(fileToDelete + ".delete"));
        } catch (Exception e) {
            if (notInTestMode) {
                logger.message("Failed to delete input file from workspace.  Failed to delete file '" + fileName
                        + "'");

            } else {

                System.out
                        .println("Failed to delete the input file from the workspace.  Failed to delete file '"
                                + fileName + "'");
            }
            throw e;
        }
    }

    return output;
}

From source file:com.apporiented.hermesftp.cmd.impl.FtpCmdRnto.java

/**
 * {@inheritDoc}//from ww w  .j  a va  2 s .  c  om
 */
public void execute() throws FtpCmdException {
    File originalPath = (File) getCtx().getAttribute(ATTR_RENAME_FILE);
    if (originalPath == null) {
        msgOut(MSG503);
        return;
    }
    getCtx().setAttribute(ATTR_RENAME_FILE, null);
    if (getArguments().length() == 0) {
        msgOut(MSG501);
        return;
    }
    String newPathName = getPathArg();
    File newPath = new File(newPathName);
    if (newPath.exists()) {
        log.debug(newPath + " already exists");
        msgOut(MSG550_EXISTS);
        return;
    }
    boolean renamed = originalPath.renameTo(newPath);
    if (!renamed) {
        msgOut(MSG451);
        return;
    }
    msgOut(MSG200);
}

From source file:org.trpr.platform.batch.impl.spring.admin.SimpleJobConfigurationService.java

/**
 * Restores the previous spring batch file, if found
 * @param jobName Name of the job//from   w ww.  j  av a2  s  .com
 */
private void restorePrevConfigFile(String jobName) {
    File configFile = new File(this.getJobConfigURI(jobName));
    if (configFile.exists()) {
        configFile.delete();
    }
    File prevFile = new File(
            this.getJobStoreURI(jobName).getPath() + SimpleJobConfigurationService.SPRING_BATCH_PREV);
    if (prevFile.exists()) {
        prevFile.renameTo(configFile);
    } else {
        this.jobXMLFile.remove(jobName);
    }
}

From source file:com.jk.framework.util.FakeRunnable.java

/**
 * Move file./*  w ww.jav a  2s .c  om*/
 *
 * @param filePath
 *            the file path
 * @param destination
 *            the destination
 */
public static void moveFile(final String filePath, final String destination) {
    final File file = new File(filePath);
    if (file.exists()) {
        final File dir = new File(destination);
        dir.mkdir();
        file.renameTo(new File(dir, file.getName()));
    }
}

From source file:net.sf.jvifm.model.FileModelManager.java

public void rename(String srcName, String destName, String path) {
    File file = new File(path);
    String parent = file.getParent();

    String newName = file.getName().replaceAll(srcName, destName);
    if (parent != null) {
        file.renameTo(new File(FilenameUtils.concat(parent, newName)));
    } else {// w  ww .jav  a2  s  .c o m
        file.renameTo(new File(newName));
    }
}

From source file:FileSplitter.java

public boolean encode() {
    try {//from  w ww . j a v a 2 s . c  o  m
        File temp = new File(f.getPath() + ".tmp");
        FileInputStream fis = new FileInputStream(f);
        FileOutputStream fos = new FileOutputStream(temp);
        byte[] buff = new byte[1024];

        int read = 0;
        while ((read = fis.read(buff)) > 0) {
            buff = FileEncoder.invertBuffer(buff, 0, read);
            fos.write(buff, 0, read);
        }

        fis.close();
        fos.flush();
        fos.close();

        f.delete();
        temp.renameTo(f);
        f = temp;
        return true;
    } catch (Exception ex) {
        ex.printStackTrace();
        return false;
    }
}

From source file:jetbrains.buildServer.vmgr.agent.Utils.java

public String[] loadVSIFFileNames(String buildID, String workPlacePath, String vSIFInputFile,
        BuildProgressLogger logger, boolean deleteInputFile) throws Exception {
    String[] output = null;/*from ww  w .ja v a2  s . co m*/
    List<String> listOfNames = new LinkedList<String>();
    BufferedReader reader = null;
    String fileName = null;
    boolean notInTestMode = true;
    if (logger == null) {
        notInTestMode = false;
    }

    // Set the right File name.
    if ("".equals(vSIFInputFile) || vSIFInputFile == null) {
        fileName = workPlacePath + File.separator + buildID + File.separator + "vsif.input";
    } else {
        fileName = vSIFInputFile;
    }

    try {

        reader = this.loadFileFromWorkSpace(buildID, workPlacePath, vSIFInputFile, logger, deleteInputFile,
                "vsif.input");
        String line = null;
        while ((line = reader.readLine()) != null) {
            listOfNames.add(line);
        }

    } catch (Exception e) {

        if (notInTestMode) {
            logger.message(
                    "Failed to read input file for the vsif targets.  Failed to load file '" + fileName + "'");
        } else {

            System.out.println("Failed to open the read file for the vsif targets.  Failed to load file '"
                    + fileName + "'");
        }

        throw e;
    } finally {
        reader.close();
    }

    Iterator<String> iter = listOfNames.iterator();
    output = new String[listOfNames.size()];
    int i = 0;
    if (notInTestMode) {
        logger.message("Found the following VSIF files for vManager launch:");
    }
    String vsiffileName = null;
    while (iter.hasNext()) {
        vsiffileName = new String(iter.next());
        output[i++] = vsiffileName;
        if (notInTestMode) {
            logger.message(i + " '" + vsiffileName + "'");
        } else {

            System.out.println(i + " '" + vsiffileName + "'");
        }
    }

    if (deleteInputFile) {
        if (notInTestMode) {
            logger.message("Job set to delete the input file.  Deleting " + fileName + "");
        }
        try {
            File fileToDelete = new File(fileName);
            fileToDelete.renameTo(new File(fileToDelete + ".delete"));
        } catch (Exception e) {
            if (notInTestMode) {
                logger.message("Failed to delete input file from workspace.  Failed to delete file '" + fileName
                        + "'");

            } else {

                System.out
                        .println("Failed to delete the input file from the workspace.  Failed to delete file '"
                                + fileName + "'");
            }
            throw e;
        }
    }

    return output;
}