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[] Args) {
    String filepath = "C:/myFile.txt";
    File aFile = new File(filepath);
    FileOutputStream outputFile = null;
    if (aFile.isFile()) {
        File newFile = aFile;//from   ww  w.  j av  a 2  s . c  om
        do {
            String name = newFile.getName();
            int period = name.indexOf('.');
            newFile = new File(newFile.getParent(),
                    name.substring(0, period) + "_old" + name.substring(period));
        } while (newFile.exists());
        aFile.renameTo(newFile);
    }
    try {
        outputFile = new FileOutputStream(aFile);
        System.out.println("myFile.txt output stream created");
    } catch (FileNotFoundException e) {
        e.printStackTrace(System.err);
    }
}

From source file:com.doculibre.constellio.utils.resources.WriteResourceBundleUtils.java

@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    File binDir = ClasspathUtils.getClassesDir();
    File projectDir = binDir.getParentFile();
    File sourceDir = new File(projectDir, "source");

    String defaultLanguage;/*  w  w  w  . ja  v  a  2  s .c om*/
    String otherLanguage;
    if (args.length > 0) {
        defaultLanguage = args[0];
        otherLanguage = args[1];
    } else {
        defaultLanguage = Locale.ENGLISH.getLanguage();
        otherLanguage = Locale.FRENCH.getLanguage();
    }

    List<File> propertiesFiles = (List<File>) FileUtils.listFiles(sourceDir, new String[] { "properties" },
            true);
    for (File propertiesFile : propertiesFiles) {
        File propertiesDir = propertiesFile.getParentFile();

        String propertiesNameWoutSuffix = StringUtils.substringBefore(propertiesFile.getName(), "_");
        propertiesNameWoutSuffix = StringUtils.substringBefore(propertiesNameWoutSuffix, ".properties");

        String noLanguageFileName = propertiesNameWoutSuffix + ".properties";
        String defaultLanguageFileName = propertiesNameWoutSuffix + "_" + defaultLanguage + ".properties";
        String otherLanguageFileName = propertiesNameWoutSuffix + "_" + otherLanguage + ".properties";

        File noLanguageFile = new File(propertiesDir, noLanguageFileName);
        File defaultLanguageFile = new File(propertiesDir, defaultLanguageFileName);
        File otherLanguageFile = new File(propertiesDir, otherLanguageFileName);

        if (defaultLanguageFile.exists() && otherLanguageFile.exists() && !noLanguageFile.exists()) {
            System.out.println(defaultLanguageFile.getPath() + " > " + noLanguageFileName);
            System.out.println(defaultLanguageFile.getPath() + " > empty file");

            defaultLanguageFile.renameTo(noLanguageFile);
            FileWriter defaultLanguageEmptyFileWriter = new FileWriter(defaultLanguageFile);
            defaultLanguageEmptyFileWriter.write("");
            IOUtils.closeQuietly(defaultLanguageEmptyFileWriter);
        }
    }
}

From source file:net.minecraftforge.fml.common.asm.transformers.AccessTransformer.java

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage: AccessTransformer <JarPath> <MapFile> [MapFile2]... ");
        System.exit(1);//w  w  w.  j a  v a 2  s  .  com
    }

    boolean hasTransformer = false;
    AccessTransformer[] trans = new AccessTransformer[args.length - 1];
    for (int x = 1; x < args.length; x++) {
        try {
            trans[x - 1] = new AccessTransformer(args[x]);
            hasTransformer = true;
        } catch (IOException e) {
            System.out.println("Could not read Transformer Map: " + args[x]);
            e.printStackTrace();
        }
    }

    if (!hasTransformer) {
        System.out.println("Could not find a valid transformer to perform");
        System.exit(1);
    }

    File orig = new File(args[0]);
    File temp = new File(args[0] + ".ATBack");
    if (!orig.exists() && !temp.exists()) {
        System.out.println("Could not find target jar: " + orig);
        System.exit(1);
    }

    if (!orig.renameTo(temp)) {
        System.out.println("Could not rename file: " + orig + " -> " + temp);
        System.exit(1);
    }

    try {
        processJar(temp, orig, trans);
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }

    if (!temp.delete()) {
        System.out.println("Could not delete temp file: " + temp);
    }
}

From source file:AllCapsDemo.java

License:asdf

public static void main(String[] arguments) {
    String sourceName = "asdf";
    try {/*w  ww .jav a 2s .c o  m*/
        File source = new File(sourceName);
        File temp = new File("cap" + sourceName + ".tmp");

        FileReader fr = new FileReader(source);
        BufferedReader in = new BufferedReader(fr);

        FileWriter fw = new FileWriter(temp);
        BufferedWriter out = new BufferedWriter(fw);

        boolean eof = false;
        int inChar = 0;
        do {
            inChar = in.read();
            if (inChar != -1) {
                char outChar = Character.toUpperCase((char) inChar);
                out.write(outChar);
            } else
                eof = true;
        } while (!eof);
        in.close();
        out.close();

        boolean deleted = source.delete();
        if (deleted)
            temp.renameTo(source);
    } catch (Exception se) {
        System.out.println("Error - " + se.toString());
    }
}

From source file:de.uni_koblenz.jgralab.utilities.converter.TGraphToTGraph2Converter.java

/**
 * Uses the apache cli interface for command line handling.
 * /* w  w w .j av a2  s .  c om*/
 * @param args
 *            the command line parameters
 */
public static void main(String[] args) {
    CommandLine cmdl = processCommandLineOptions(args);
    try {

        String inputFilename = cmdl.hasOption('i') ? cmdl.getOptionValue('i') : null;
        String outputFilename = cmdl.hasOption('o') ? cmdl.getOptionValue('o') : null;
        boolean loadSchema = outputFilename != null && cmdl.hasOption('l');

        String tempFilename = outputFilename != null
                ? outputFilename + "~" + Long.toString(System.currentTimeMillis())
                : null;

        File inputFile = inputFilename != null ? new File(inputFilename) : null;
        File tempFile = tempFilename != null ? new File(tempFilename) : null;
        File outputFile = outputFilename != null ? new File(outputFilename) : null;

        InputStream in = inputFile != null ? new FileInputStream(inputFile) : System.in;

        OutputStream out = tempFilename != null ? new FileOutputStream(tempFile) : System.out;

        TGraphToTGraph2Converter converter = new TGraphToTGraph2Converter();
        converter.convertTGStream(out, in);

        if (!tempFile.renameTo(outputFile)) {
            System.err.println("Warning: temporary file could not be moved to\n" + outputFile.getAbsolutePath()
                    + "\nit can be found at\n" + tempFile.getAbsolutePath());
        }

        System.out.println("Fini.");
        if (loadSchema) {
            loadConvertedSchema(outputFilename);
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:net.minecraftforge.fml.common.asm.transformers.MarkerTransformer.java

public static void main(String[] args) {
    if (args.length < 2) {
        System.out.println("Usage: MarkerTransformer <JarPath> <MapFile> [MapFile2]... ");
        return;//w w w.ja va2 s . co m
    }

    boolean hasTransformer = false;
    MarkerTransformer[] trans = new MarkerTransformer[args.length - 1];
    for (int x = 1; x < args.length; x++) {
        try {
            trans[x - 1] = new MarkerTransformer(args[x]);
            hasTransformer = true;
        } catch (IOException e) {
            System.out.println("Could not read Transformer Map: " + args[x]);
            e.printStackTrace();
        }
    }

    if (!hasTransformer) {
        System.out.println("Could not find a valid transformer to perform");
        return;
    }

    File orig = new File(args[0]);
    File temp = new File(args[0] + ".ATBack");
    if (!orig.exists() && !temp.exists()) {
        System.out.println("Could not find target jar: " + orig);
        return;
    }
    /*
            if (temp.exists())
            {
    if (orig.exists() && !orig.renameTo(new File(args[0] + (new SimpleDateFormat(".yyyy.MM.dd.HHmmss")).format(new Date()))))
    {
        System.out.println("Could not backup existing file: " + orig);
        return;
    }
    if (!temp.renameTo(orig))
    {
        System.out.println("Could not restore backup from previous run: " + temp);
        return;
    }
            }
    */
    if (!orig.renameTo(temp)) {
        System.out.println("Could not rename file: " + orig + " -> " + temp);
        return;
    }

    try {
        processJar(temp, orig, trans);
    } catch (IOException e) {
        e.printStackTrace();
    }

    if (!temp.delete()) {
        System.out.println("Could not delete temp file: " + temp);
    }
}

From source file:eu.ensure.aging.AgingSimulator.java

public static void main(String[] args) {

    Options options = new Options();
    options.addOption("n", "flip-bit-in-name", true, "Flip bit in first byte of name of first file");
    options.addOption("u", "flip-bit-in-uname", true, "Flip bit in first byte of username of first file");
    options.addOption("c", "update-checksum", true, "Update header checksum (after prior bit flips)");

    Properties properties = new Properties();
    CommandLineParser parser = new PosixParser();
    try {//from w  w  w.j a v a  2s  .com
        CommandLine line = parser.parse(options, args);

        //
        if (line.hasOption("flip-bit-in-name")) {
            properties.put("flip-bit-in-name", line.getOptionValue("flip-bit-in-name"));
        } else {
            // On by default (if not explicitly de-activated above)
            properties.put("flip-bit-in-name", "true");
        }

        //
        if (line.hasOption("flip-bit-in-uname")) {
            properties.put("flip-bit-in-uname", line.getOptionValue("flip-bit-in-uname"));
        } else {
            properties.put("flip-bit-in-uname", "false");
        }

        //
        if (line.hasOption("update-checksum")) {
            properties.put("update-checksum", line.getOptionValue("update-checksum"));
        } else {
            properties.put("update-checksum", "false");
        }

        String[] fileArgs = line.getArgs();

        if (fileArgs.length < 1) {
            printHelp(options, System.err);
            System.exit(1);
        }

        String srcPath = fileArgs[0];

        File srcAIP = new File(srcPath);
        if (!srcAIP.exists()) {
            String info = "The source path does not locate a file: " + srcPath;
            System.err.println(info);
            System.exit(1);
        }

        if (srcAIP.isDirectory()) {
            String info = "The source path locates a directory, not a file: " + srcPath;
            System.err.println(info);
            System.exit(1);
        }

        if (!srcAIP.canRead()) {
            String info = "Cannot read source file: " + srcPath;
            System.err.println(info);
            System.exit(1);
        }

        boolean doReplace = false;
        File destAIP = null;

        if (fileArgs.length > 1) {
            String destPath = fileArgs[1];
            destAIP = new File(destPath);

            if (destAIP.exists()) {
                String info = "The destination path locates an existing file: " + destPath;
                System.err.println(info);
                System.exit(1);
            }
        } else {
            doReplace = true;
            try {
                destAIP = File.createTempFile("tmp-aged-aip-", ".tar");
            } catch (IOException ioe) {
                String info = "Failed to create temporary file: ";
                info += ioe.getMessage();
                System.err.println(info);
                System.exit(1);
            }
        }

        String info = "Age simulation\n";
        info += "  Reading from " + srcAIP.getName() + "\n";
        if (doReplace) {
            info += "  and replacing it's content";
        } else {
            info += "  Writing to " + destAIP.getName();
        }
        System.out.println(info);

        //
        InputStream is = null;
        OutputStream os = null;
        try {
            is = new BufferedInputStream(new FileInputStream(srcAIP));
            os = new BufferedOutputStream(new FileOutputStream(destAIP));

            simulateAging(srcAIP.getName(), is, os, properties);

        } catch (FileNotFoundException fnfe) {
            info = "Could not locate file: " + fnfe.getMessage();
            System.err.println(info);
        } finally {
            try {
                if (null != os)
                    os.close();
                if (null != is)
                    is.close();
            } catch (IOException ignore) {
            }
        }

        //
        if (doReplace) {
            File renamedOriginal = new File(srcAIP.getName() + "-backup");
            srcAIP.renameTo(renamedOriginal);

            ReadableByteChannel rbc = null;
            WritableByteChannel wbc = null;
            try {
                rbc = Channels.newChannel(new FileInputStream(destAIP));
                wbc = Channels.newChannel(new FileOutputStream(srcAIP));
                FileIO.fastChannelCopy(rbc, wbc);
            } catch (FileNotFoundException fnfe) {
                info = "Could not locate temporary output file: " + fnfe.getMessage();
                System.err.println(info);
            } catch (IOException ioe) {
                info = "Could not copy temporary output file over original AIP: " + ioe.getMessage();
                System.err.println(info);
            } finally {
                try {
                    if (null != wbc)
                        wbc.close();
                    if (null != rbc)
                        rbc.close();

                    destAIP.delete();
                } catch (IOException ignore) {
                }
            }
        }
    } catch (ParseException pe) {
        String info = "Failed to parse command line: " + pe.getMessage();
        System.err.println(info);
        printHelp(options, System.err);
        System.exit(1);
    }
}

From source file:Main.java

/**
 * Set a track as complete./*from w ww  .j  ava2 s .  co  m*/
 * @param file File
 * @return True if the operation was successful
 */
public static boolean setComplete(File file) {
    return file.renameTo(new File(file.getAbsolutePath() + ".complete"));
}

From source file:Main.java

public static boolean reNamePath(String oldName, String newName) {
    File f = new File(oldName);
    return f.renameTo(new File(newName));
}

From source file:Main.java

public static boolean rename(File file, String name) {
    return file.renameTo(new File(file.getParent(), name));
}