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.twinsoft.convertigo.beans.core.MobilePlatform.java

private void checkFolder(String oldName) {
    if (oldName != null && !oldName.equals(getName())) {
        File oldFolder = new File(getProject().getDirPath() + "/DisplayObjects/platforms/" + oldName);
        if (oldFolder.exists()) {
            File newFolder = getResourceFolder();
            if (!newFolder.exists()) {
                oldFolder.renameTo(newFolder);
            } else {
                Engine.logBeans.warn("(MobilePlatform) The folder '" + oldFolder.getAbsolutePath()
                        + "' cannot be moved to the existing '" + newFolder.getAbsolutePath() + "' folder");
            }//w w  w . j a v  a 2 s  . c o m
            return;
        }
    }
    checkFolder();
}

From source file:com.meltmedia.cadmium.core.github.ApiClientTest.java

@After
public void destroyAfter() throws Exception {

    File cadmiumDir = new File(System.getProperty("user.home"), ".cadmium");
    File tokenFile = new File(cadmiumDir, "github.token");
    if (tokenFile.exists()) {
        tokenFile.delete();//from  w w w.j a v  a 2  s  .  c  om
    }
    tokenFile = new File(cadmiumDir, "github.token.old");

    if (tokenFile.exists()) {
        tokenFile.renameTo(new File(cadmiumDir, "github.token"));
    }

    if (tokenAuth != null) {
        ApiClient client = new ApiClient(tokenAuth.getToken());
        client.deauthorizeToken(username, password, tokenAuth.getId());
        tokenAuth = null;
    }
}

From source file:fedora.utilities.install.Installer.java

private File buildWAR() throws InstallationFailedException {
    String fedoraWarName = _opts.getValue(InstallOptions.FEDORA_APP_SERVER_CONTEXT);
    System.out.println("Preparing " + fedoraWarName + ".war...");
    // build a staging area in FEDORA_HOME
    try {/* w  w w . ja v a 2  s.  c  o  m*/
        File warStage = new File(installDir, "fedorawar" + File.separator);
        warStage.mkdirs();
        Zip.unzip(_dist.get(Distribution.FEDORA_WAR), warStage);

        // modify web.xml
        System.out.println("Processing web.xml");
        File distWebXML = new File(warStage, "WEB-INF/web.xml");
        FedoraWebXML webXML = new FedoraWebXML(distWebXML.getAbsolutePath(), _opts);
        Writer outputWriter = new BufferedWriter(new FileWriter(distWebXML));
        webXML.write(outputWriter);
        outputWriter.close();

        // Remove commons-collections, commons-dbcp, and commons-pool
        // from fedora.war if using Tomcat 5.0
        String container = _opts.getValue(InstallOptions.SERVLET_ENGINE);
        File webinfLib = new File(warStage, "WEB-INF/lib/");
        if (container.equals(InstallOptions.INCLUDED) || container.equals(InstallOptions.EXISTING_TOMCAT)) {
            File tomcatHome = new File(_opts.getValue(InstallOptions.TOMCAT_HOME));
            File dbcp55 = new File(tomcatHome, "common/lib/naming-factory-dbcp.jar");
            File dbcp6 = new File(tomcatHome, "lib/tomcat-dbcp.jar");

            if (!dbcp55.exists() && !dbcp6.exists()) {
                new File(webinfLib, Distribution.COMMONS_COLLECTIONS).delete();
                new File(webinfLib, Distribution.COMMONS_DBCP).delete();
                new File(webinfLib, Distribution.COMMONS_POOL).delete();
                // JDBC driver installation into common/lib for Tomcat 5.0 is
                // handled by ExistingTomcat50
            } else {
                installJDBCDriver(_dist, _opts, webinfLib);
            }
        } else {
            installJDBCDriver(_dist, _opts, webinfLib);
        }

        // Remove log4j if using JBoss Application Server
        if (container.equals(InstallOptions.OTHER)
                && _opts.getValue(InstallOptions.USING_JBOSS).equals("true")) {
            new File(webinfLib, Distribution.LOG4J).delete();
        }

        // FeSL configuration
        if (_opts.getBooleanValue(InstallOptions.FESL_ENABLED, false)) {
            File originalWsdd = new File(warStage, "WEB-INF/server-config.wsdd");
            originalWsdd.renameTo(new File(warStage, "WEB-INF/server-config.wsdd.backup.original"));

            File feslWsdd = new File(warStage, "WEB-INF/melcoe-pep-server-config.wsdd");
            feslWsdd.renameTo(new File(warStage, "WEB-INF/server-config.wsdd"));
        }

        File fedoraWar = new File(installDir, fedoraWarName + ".war");
        Zip.zip(fedoraWar, warStage.listFiles());
        return fedoraWar;

    } catch (FileNotFoundException e) {
        throw new InstallationFailedException(e.getMessage(), e);
    } catch (IOException e) {
        throw new InstallationFailedException(e.getMessage(), e);
    }
}

From source file:github.daneren2005.dsub.util.Util.java

public static void renameFile(File from, File to) throws IOException {
    if (!from.renameTo(to)) {
        Log.i(TAG, "Failed to rename " + from + " to " + to);
    }//from  ww w. j  a  v  a 2  s  . c o m
}

From source file:com.proofpoint.event.collector.S3Uploader.java

private File moveFile(File file, File targetDirectory) {
    File targetFile = new File(targetDirectory, file.getName());
    try {//from   ww  w .j a v  a 2s . com
        if (!file.renameTo(targetFile)) {
            log.error("Error renaming file %s to %s", file, targetFile);
        }
    } catch (Exception e) {
        log.error("Error renaming file %s to %s", file, targetFile);
    }
    return targetFile;
}

From source file:cn.isif.util_plus.cache.LruDiskCache.java

private static void renameTo(File from, File to, boolean deleteDestination) throws IOException {
    if (deleteDestination) {
        deleteIfExists(to);//  w  w w .ja  va 2 s.  c  om
    }
    if (!from.renameTo(to)) {
        throw new IOException();
    }
}

From source file:com.thejoshwa.ultrasonic.androidapp.util.Util.java

public static void renameFile(File from, File to) throws IOException {
    if (from.renameTo(to)) {
        Log.i(TAG, "Renamed " + from + " to " + to);
    } else {/*from  w  ww .j a  va2s. co  m*/
        atomicCopy(from, to);
    }
}

From source file:controller.uploadProductController.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/*from  ww  w  .ja  v  a 2s. co m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HttpSession session = request.getSession(true);
    String userPath = request.getServletPath();
    if (userPath.equals("/uploadProduct")) {
        boolean success = true;
        // configures upload settings
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // sets memory threshold - beyond which files are stored in disk
        factory.setSizeThreshold(MEMORY_THRESHOLD);
        // sets temporary location to store files
        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

        ServletFileUpload upload = new ServletFileUpload(factory);

        // sets maximum size of upload file
        upload.setFileSizeMax(MAX_FILE_SIZE);

        // sets maximum size of request (include file + form data)
        upload.setSizeMax(MAX_REQUEST_SIZE);

        // constructs the directory path to store upload file
        // this path is relative to application's directory
        String uploadPath = getServletContext().getInitParameter("upload.location");
        //creates a HashMap of all inputs
        HashMap hashMap = new HashMap();
        String productId = UUID.randomUUID().toString();
        try {
            @SuppressWarnings("unchecked")
            List<FileItem> formItems = upload.parseRequest(request);
            for (FileItem item : formItems) {
                // processes only fields that are not form fields
                if (!item.isFormField()) {
                    File file = new File(item.getName());
                    File file2 = new File(productId + ".jpg");
                    String filePath = uploadPath + File.separator + file.getName();
                    // saves the file on disk
                    File storeFile = new File(filePath);
                    item.write(storeFile);
                    File rename = new File(filePath);
                    boolean flag = rename.renameTo(file2);
                } else {
                    hashMap.put(item.getFieldName(), item.getString());
                }
            }
        } catch (FileUploadException ex) {
            Logger.getLogger(uploadProductController.class.getName()).log(Level.SEVERE, null, ex);
            request.setAttribute("error", true);
            request.setAttribute("errorMessage", ex.getMessage());
            success = false;
        } catch (Exception ex) {
            Logger.getLogger(uploadProductController.class.getName()).log(Level.SEVERE, null, ex);
            request.setAttribute("error", true);
            request.setAttribute("errorMessage", ex.getMessage());
            success = false;
        }
        String owner = (String) session.getAttribute("customerEmail");
        if (owner == null) {
            owner = "shop";
        }

        String pName = hashMap.get("pName").toString();
        String mNo = hashMap.get("mNo").toString();
        String brand = hashMap.get("brand").toString();
        String description = hashMap.get("description").toString();
        String quantity = hashMap.get("quantity").toString();
        String price = hashMap.get("price").toString();
        String addInfo = hashMap.get("addInfo").toString();
        int categoryId = Integer.parseInt(hashMap.get("category").toString());

        ProductDAO productDAO = new ProductDAOImpl();
        Product product;

        try {
            double priceDouble = Double.parseDouble(price);
            int quantityInt = Integer.parseInt(quantity);
            Category category = (new CategoryDAOImpl()).getCategoryFromID(categoryId);
            if (owner.equals("shop")) {
                product = new Product(productId, pName, mNo, category, quantityInt, priceDouble, brand,
                        description, addInfo, true, true, owner);
            } else {
                product = new Product(productId, pName, mNo, category, quantityInt, priceDouble, brand,
                        description, addInfo, false, false, owner);
            }

            if (!(productDAO.addProduct(product, category.getName()))) {
                throw new Exception("update unsuccessful");
            }
        } catch (Exception e) {
            request.setAttribute("error", true);
            request.setAttribute("errorMessage", e.getMessage());
            success = false;
        }

        request.setAttribute("pName", pName);
        request.setAttribute("mNo", mNo);
        request.setAttribute("brand", brand);
        request.setAttribute("description", description);
        request.setAttribute("quantity", quantity);
        request.setAttribute("price", price);
        request.setAttribute("addInfo", addInfo);
        request.setAttribute("categoryId", categoryId);
        request.setAttribute("success", success);
        if (success == true) {
            request.setAttribute("error", false);
            request.setAttribute("errorMessage", null);
        }
        String url;
        if (owner.equals("shop")) {
            url = "/WEB-INF/view/adminAddProducts.jsp";
        } else {
            url = "/WEB-INF/view/clientSideView/addRecycleProduct.jsp";
        }
        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (ServletException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

    }

}

From source file:net.ftb.util.FileUtils.java

/**
 * deletes the META-INF/*from  w  w w .  ja  v a 2  s.c  om*/
 */
public static void killMetaInf() {
    File inputFile = new File(Settings.getSettings().getInstallPath() + "/" + ModPack.getSelectedPack().getDir()
            + "/minecraft/bin", "minecraft.jar");
    File outputTmpFile = new File(Settings.getSettings().getInstallPath() + "/"
            + ModPack.getSelectedPack().getDir() + "/minecraft/bin", "minecraft.jar.tmp");
    try {
        JarInputStream input = new JarInputStream(new FileInputStream(inputFile));
        JarOutputStream output = new JarOutputStream(new FileOutputStream(outputTmpFile));
        JarEntry entry;

        while ((entry = input.getNextJarEntry()) != null) {
            if (entry.getName().contains("META-INF")) {
                continue;
            }
            output.putNextEntry(entry);
            byte buffer[] = new byte[1024];
            int amo;
            while ((amo = input.read(buffer, 0, 1024)) != -1) {
                output.write(buffer, 0, amo);
            }
            output.closeEntry();
        }

        input.close();
        output.close();

        if (!inputFile.delete()) {
            Logger.logError("Failed to delete Minecraft.jar.");
            return;
        }
        outputTmpFile.renameTo(inputFile);
    } catch (FileNotFoundException e) {
        Logger.logError("Error while killing META-INF", e);
    } catch (IOException e) {
        Logger.logError("Error while killing META-INF", e);
    }
}

From source file:com.wavemaker.StudioInstallService.java

private void moveFiles(File zipFolder, File zipFile) throws Exception {
    File versionFile = new File(zipFolder, "version.txt");
    if (!versionFile.exists())
        throw new IOException("This repo.zip file does not contain the correct version information.");
    String s = IOUtils.read(versionFile);
    if (s.indexOf("6.6") != 0)
        throw new IOException("This repo.zip file is from the wrong version of studio");
    File webapproot = new File(RuntimeAccess.getInstance().getSession().getServletContext().getRealPath(""));

    File ace = new File(zipFolder, "ace");
    File newAce = new File(webapproot, "../studio/app/lib/ace");
    ace.renameTo(newAce);
    if (!newAce.exists()) {
        System.out.println("FAILED TO WRITE: " + newAce.getAbsolutePath());
    }//w ww . java2 s.  c  o  m

    File h1 = new File(zipFolder, "hibernate-tools-3.2.4.GA.jar");
    if (!h1.renameTo(new File(webapproot, "../studio/WEB-INF/lib/hibernate-tools-3.2.4.GA.jar"))) {
        System.out.println("FAILED TO WRITE: "
                + new File(webapproot, "../studio/WEB-INF/lib/hibernate-tools.jar").getAbsolutePath());
    }

    File h2 = new File(zipFolder, "hibernate-3.2.4.sp1.jar");
    if (!h2.renameTo(new File(webapproot, "../studio/WEB-INF/lib/hibernate-3.2.4.sp1.jar"))) {
        System.out.println("FAILED TO WRITE: "
                + new File(webapproot, "../studio/WEB-INF/lib/hibernate3.jar").getAbsolutePath());
    }

    File jtds = new File(zipFolder, "jtds-1.2.2.jar");
    if (!jtds.renameTo(new File(webapproot, "../studio/WEB-INF/lib/jtds-1.2.2.jar"))) {
        System.out.println("FAILED TO WRITE: "
                + new File(webapproot, "../studio/WEB-INF/lib/jtds-1.2.2.jar").getAbsolutePath());
    }

    zipFile.renameTo(new File(webapproot, "../studio/installed_bundle.zip"));

    try {
        restartStudioApp();
    } catch (Exception e) {
        e.printStackTrace();
        throw new Exception("Failed to restart Studio after loading jars.  Please restart Studio manually.");
    }
    /*
    try {
     IOUtils.deleteRecursive(zipFolder);
    } catch(Exception e){}
    */
    //return result;
}