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:MainClass.java

public static void main(String[] a) {
    File file = new File("c:\\text.txt");
    file.renameTo(new File("c:\\text.txt.bak"));
}

From source file:Main.java

public static void main(String[] args) {
    File oldName = new File("C:/s.txt");
    File newName = new File("C:/d.txt");

    if (oldName.renameTo(newName))
        System.out.println("File has been renamed");
    else/*from  w  ww.  j  a va  2 s  .c  o m*/
        System.out.println("Error renaming the file");
}

From source file:Main.java

public static void main(String[] args) {
    File oldName = new File("C:/s.txt");
    File newName = new File("C:/d.txt");

    if (oldName.renameTo(newName)) {
        System.out.println("renamed");
    } else {//ww w  .  j a  v  a  2 s  .c o m
        System.out.println("Error");
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    File dir = new File("directoryname");

    boolean success = file.renameTo(new File(dir, file.getName()));
    if (!success) {
        System.out.println("File was not successfully moved");
    }//from w w  w.  jav  a2  s  .c  o  m
}

From source file:MainClass.java

public static void main(String args[]) {

    try {/*from  w  ww .jav  a  2 s  .c o  m*/

        File oldFile = new File(args[0]);

        File newFile = new File(args[1]);

        boolean result = oldFile.renameTo(newFile);

        System.out.println(result);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] argv) {
    // Rename old-dummy.txt to new_dummy.txt
    File oldFile = new File("old_dummy.txt");
    File newFile = new File("new_dummy.txt");

    boolean fileRenamed = oldFile.renameTo(newFile);
    if (fileRenamed) {
        System.out.println(oldFile + "  renamed  to " + newFile);
    } else {/*from   w w w .  ja va  2 s  .  c o m*/
        System.out.println("Renaming " + oldFile + "  to " + newFile + "  failed.");
    }

}

From source file:Main.java

public static void main(String[] args) {

    // create new File objects
    File f = new File("C:/test.txt");
    File f1 = new File("C:/testA.txt");

    // rename file
    boolean bool = f.renameTo(f1);

    // print/*from  w ww. ja v  a  2s  .c  om*/
    System.out.print("File renamed? " + bool);

}

From source file:MakeDirectories.java

public static void main(String[] args) {
    if (args.length < 1)
        usage();/*from   w  w w  . ja  va  2 s . co  m*/
    if (args[0].equals("-r")) {
        if (args.length != 3)
            usage();
        File old = new File(args[1]), rname = new File(args[2]);
        old.renameTo(rname);
        fileData(old);
        fileData(rname);
        return; // Exit main
    }
    int count = 0;
    boolean del = false;
    if (args[0].equals("-d")) {
        count++;
        del = true;
    }
    count--;
    while (++count < args.length) {
        File f = new File(args[count]);
        if (f.exists()) {
            System.out.println(f + " exists");
            if (del) {
                System.out.println("deleting..." + f);
                f.delete();
            }
        } else { // Doesn't exist
            if (!del) {
                f.mkdirs();
                System.out.println("created " + f);
            }
        }
        fileData(f);
    }
}

From source file:Rename.java

public static void main(String[] argv) throws IOException {

    // Construct the file object. Does NOT create a file on disk!
    File f = new File("Rename.java~"); // backup of this source file.

    // Rename the backup file to "junk.dat"
    // Renaming requires a File object for the target.
    f.renameTo(new File("junk.dat"));
}

From source file:ProductionJS.java

/**
 * @param args/*from   ww  w  . j  a  va 2s.c  o m*/
 */
public static void main(String[] args) {
    boolean precondition = true;
    BackupLog backupLog = new BackupLog("log/backupLog.txt");
    Property properties = null;
    Settings settings = null;

    // Load main cfg file
    try {
        properties = new Property("cfg/cfg");
    } catch (IOException e) {
        backupLog.log("ERROR : Config file not found");
        precondition = false;
    }

    // Load cfg for Log4J
    if (precondition) {
        try {
            DOMConfigurator.configureAndWatch(properties.getProperty("loggerConfiguration"));
            logger.info("ProductionJS is launched\n_______________________");
        } catch (Exception e) {
            backupLog.log(e.getMessage());
            precondition = false;
        }
    }

    // Load settings for current execution
    if (precondition) {
        try {
            settings = new Settings(new File(properties.getProperty("importConfiguration")));
        } catch (IOException e) {
            logger.error("Properties file not found");
            precondition = false;
        } catch (org.json.simple.parser.ParseException e) {
            logger.error("Properties file reading error : JSON may be invalid, check it!");
            precondition = false;
        }
    }

    // All is OK : GO!
    if (precondition) {
        logger.info("Configuration OK");
        logger.info("\"_______________________\nConcat BEGIN\n_______________________\n");

        Concat concat = new Concat();
        try {
            if (settings.getPrior() != null) {
                logger.info("Add Prior files\n_______________________\n");
                concat.addJSONArray(settings.getPrior());
            }

            for (int i = 0; i < settings.getIn().size(); i++) {
                ProductionJS.logger.info("Importation number " + (i + 1));
                ProductionJS.logger.info("Directory imported " + settings.getIn().get(i));

                Directory dir = new Directory(new File(settings.getIn().get(i).toString()));
                dir.scan();
                concat.add(dir.getFiles());
            }
            concat.output(settings.getOut());

            logger.info("\"_______________________\nConcat END\n_______________________\n");
            if (settings.getMinify() != null && settings.getMinify() == true) {
                logger.info(
                        "\"_______________________\nMinify of concatened file BEGIN\n_______________________\n");
                new Minify(new File(settings.getOut()), settings.getOut());
                logger.info(
                        "\"_______________________\nMinify of concatened file END\n_______________________\n");
            }

            if (settings.getLicense() != null) {
                logger.info("Add License\n_______________________\n");
                concat = new Concat();
                UUID uniqueID = UUID.randomUUID();

                PrintWriter tmp = new PrintWriter(uniqueID.toString() + ".txt");
                tmp.println(settings.getLicense());
                tmp.close();
                File license = new File(uniqueID.toString() + ".txt");
                concat.add(license);

                File out = new File(settings.getOut());
                File tmpOut = new File(uniqueID + out.getName());
                out.renameTo(tmpOut);
                concat.add(tmpOut);

                concat.output(settings.getOut());
                license.delete();
                tmpOut.delete();
            }
        } catch (IOException e) {
            StackTrace stackTrace = new StackTrace(e.getStackTrace());
            logger.error("An error occurred " + e.getMessage() + " " + stackTrace.toString());
        }
    }
}