Java File Rename renameFile(String oldname, String newname)

Here you can find the source of renameFile(String oldname, String newname)

Description

rename File

License

LGPL

Declaration

public static boolean renameFile(String oldname, String newname) 

Method Source Code


//package com.java2s;
/*// ww  w.  ja v a2  s.co  m
 * Copyright (C) 2001-2004 Gaudenz Alder
 * 
 * 6/01/2006: I, Raphpael Valyi, changed back the header of this file to LGPL
 * because nobody changed the file significantly since the last
 * 3.0 version of GPGraphpad that was LGPL. By significantly, I mean: 
 *  - less than 3 instructions changes could honnestly have been done from an old fork,
 *  - license or copyright changes in the header don't count
 *  - automaticaly updating imports don't count,
 *  - updating systematically 2 instructions to a library specification update don't count.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
    
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
    
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

import java.io.File;

public class Main {
    public static boolean renameFile(String oldname, String newname) {
        try {

            // File (or directory) with old name
            File file = new File(oldname);

            // File (or directory) with new name
            File file2 = new File(newname);

            if (file2.exists()) {
                return false;
            }

            // Rename file (or directory)
            return file.renameTo(file2);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
            return false;
        }

    }
}

Related

  1. renameFile(String fromFilename, String toFilename)
  2. renameFile(String newName, String oldName)
  3. renameFile(String oldFile, String newName)
  4. renameFile(String oldFileName, String newFileName)
  5. renameFile(String oldname, String newname)
  6. renameFile(String oldname, String newone)
  7. renameFile(String oldPath, String newName)
  8. renameFile(String oldPath, String newPath)
  9. renameFile(String oldPath, String newPath, boolean overwrite)