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

/**
 * Replace a legacy jboss header with the current default header
 * //from w w  w  .  j  ava  2 s.c om
 * @param javaFile -
 *          the java source file
 * @param endOfHeader -
 *          the offset to the end of the legacy header
 * @throws IOException -
 *           thrown on failure to replace the header
 */
static void replaceHeader(File javaFile, long endOfHeader) throws IOException {
    if (log.isLoggable(Level.FINE))
        log.fine("Replacing legacy jboss header in: " + javaFile);
    RandomAccessFile raf = new RandomAccessFile(javaFile, "rw");
    File bakFile = new File(javaFile.getAbsolutePath() + ".bak");
    FileOutputStream fos = new FileOutputStream(bakFile);
    fos.write(DEFAULT_HEADER.getBytes());
    FileChannel fc = raf.getChannel();
    long count = raf.length() - endOfHeader;
    fc.transferTo(endOfHeader, count, fos.getChannel());
    fc.close();
    fos.close();
    raf.close();
    if (javaFile.delete() == false)
        log.severe("Failed to delete java file: " + javaFile);
    if (bakFile.renameTo(javaFile) == false)
        throw new SyncFailedException("Failed to replace: " + javaFile);
}

From source file:com.likya.myra.jef.utils.JobQueueOperations.java

public static boolean persistDisabledJobQueue(ConfigurationManager configurationManager,
        HashMap<String, String> jobQueue) {

    FileOutputStream fos = null;/*from  ww w  . jav a 2s .c  om*/
    ObjectOutputStream out = null;

    if (jobQueue.size() == 0) {
        return true;
    }

    try {

        File fileTemp = new File(configurationManager.getFileToPersist() + "_disabled.temp"); //$NON-NLS-1$
        fos = new FileOutputStream(fileTemp);

        out = new ObjectOutputStream(fos);

        out.writeObject(jobQueue);
        out.close();

        File file = new File(configurationManager.getFileToPersist() + "_disabled");

        if (file.exists()) {
            file.delete();
        }

        fileTemp.renameTo(file);

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return true;

}

From source file:controller.InputSanitizer.java

private static String checkAttachmentForPictures(String description, String uploadDir)
        throws NoSuchAlgorithmException, FileNotFoundException, IOException {
    String modifiedDescription = "";

    if (description.contains("src=\"/uploads/")) // suche Bilder
    {/*from   w  ww.j  a  va2  s. c  o m*/
        // die Schleife ist notwenidig falls mehrere Dateien hinzugeuegt werden
        // andernfalls wuerde nur die erste Datei einen MD5 Title erhalten
        String[] splitDescription = description.split("<img");

        String newSourceString = "src=\"/" + uploadDir;
        if (uploadDir.equals("webapps/uploads/")) {
            newSourceString = "src=\"/uploads/";
        }

        for (String img : splitDescription) {
            if (img.contains("src=\"/uploads/")) {
                img = img.replace("src=\"/uploads/", newSourceString);

                int indexBegin = img.indexOf(newSourceString) + newSourceString.length();
                int indexEnd = img.indexOf("\"", indexBegin + 1);
                String oldFilename = img.substring(indexBegin, indexEnd);
                String oldFilePath = uploadDir + oldFilename;

                MessageDigest md = MessageDigest.getInstance("MD5");
                String digest = getDigest(new FileInputStream(oldFilePath), md, 2048);

                if (!oldFilename.equals(digest)) {
                    String oldFileBigPath = uploadDir + oldFilename + "_big";
                    String bigDigest = getDigest(new FileInputStream(oldFileBigPath), md, 2048);

                    String newFilePath = uploadDir + digest;
                    String newFileBigPath = uploadDir + bigDigest;

                    // definiere thumbnail als newFile und neue Datei md5File
                    File oldFile = new File(oldFilePath);
                    File md5File = new File(newFilePath); // new File

                    // definiere Original File (thumbnail) als newFile und neue Datei md5File
                    File oldFileOrg = new File(oldFileBigPath);
                    File md5FileOrg = new File(newFileBigPath); // new File

                    // Benenne Files um
                    if (oldFile.renameTo(md5File)) {
                        img = img.replace(oldFilename, digest);
                    }

                    String linkToBigPictureBegin = "";
                    String linkToBigPictureEnd = "";

                    if (oldFileOrg.renameTo(md5FileOrg)) {
                        newSourceString = "href=\"" + uploadDir;
                        if (uploadDir.equals("webapps/uploads/")) // if real server use this
                        {
                            newSourceString = "href=\"/uploads/";
                        }

                        linkToBigPictureBegin = "<a " + newSourceString + bigDigest + "\" target=\"_blank\">";
                        linkToBigPictureEnd = "</a>";
                    }

                    img = linkToBigPictureBegin + "<img " + img + linkToBigPictureEnd;
                    System.out.println(img);
                } else {
                    System.out.println(img);
                    img = "<img" + img;
                }
            } else {
                if (!description.startsWith(img)) {
                    img = "<img" + img;
                }
            }
            modifiedDescription = modifiedDescription + img;
        }
    } else // no images => no changes
    {
        modifiedDescription = description;
    }
    return modifiedDescription;
}

From source file:edu.stanford.epad.common.util.EPADFileUtils.java

/**
 * User this instead of write when a file will be over-written often.
 * /*from   w  w  w. jav  a2s . c  o  m*/
 * @param file File the file to over-write.
 * @param contents String
 * @return boolean
 */
public static boolean overwrite(File file, String contents) {
    String tempFilename = file.getAbsoluteFile() + "." + UUID.randomUUID().toString() + ".tmp";
    File tempFile = new File(tempFilename);

    return write(tempFile, contents) && tempFile.renameTo(file);
}

From source file:com.mycompany.trafficimportfileconverter2.Main2Controller.java

public static boolean rename(String oldFileName, String newFileName) {
    new File(newFileName).delete();
    File oldFile = new File(oldFileName);
    return oldFile.renameTo(new File(newFileName));
}

From source file:edu.stanford.epad.common.util.EPADFileUtils.java

public static File renameFile(File file, String newName) throws Exception {
    File newFile = new File(file.getParent(), newName);
    file.renameTo(newFile);
    return newFile;
}

From source file:net.sourceforge.kalimbaradio.androidapp.util.Util.java

public static void atomicCopy(File from, File to) throws IOException {
    FileInputStream in = null;/*from ww  w  .  java 2s .  c om*/
    FileOutputStream out = null;
    File tmp = null;
    try {
        tmp = new File(to.getPath() + ".tmp");
        in = new FileInputStream(from);
        out = new FileOutputStream(tmp);
        in.getChannel().transferTo(0, from.length(), out.getChannel());
        out.close();
        if (!tmp.renameTo(to)) {
            throw new IOException("Failed to rename " + tmp + " to " + to);
        }
        LOG.info("Copied " + from + " to " + to);
    } catch (IOException x) {
        close(out);
        delete(to);
        throw x;
    } finally {
        close(in);
        close(out);
        delete(tmp);
    }
}

From source file:com.buaa.cfs.io.nativeio.NativeIO.java

/**
 * A version of renameTo that throws a descriptive exception when it fails.
 *
 * @param src The source path//w  ww. j  a  v  a2  s. co m
 * @param dst The destination path
 *
 * @throws NativeIOException On failure.
 */
public static void renameTo(File src, File dst) throws IOException {
    if (!nativeLoaded) {
        if (!src.renameTo(dst)) {
            throw new IOException("renameTo(src=" + src + ", dst=" + dst + ") failed.");
        }
    } else {
        renameTo0(src.getAbsolutePath(), dst.getAbsolutePath());
    }
}

From source file:com.ieasy.basic.util.file.FileUtils.java

/**
 * ??? <b>function:</b> type??????type
 * <p>/*w w  w .j a  v a  2s  . c om*/
 * example: type = html index.html.html -> index.html
 * </p>
 * <p>
 * example: type = zh_CN index.html.zh_CN -> index.html
 * </p>
 * <p>
 * batchRename("F:\\server\\chat-tomcat-7.0.32\\webapps\\jwchat", "zh_CN");
 * </p>
 * 
 * @author hoojo
 * @createDate 2012-5-16 ?02:16:48
 * @param path
 * @param type
 * @throws Exception
 */
public static void batchRename(String path, String type) throws Exception {
    if (path == null || "".equals(path)) {
        throw new Exception("???");
    }
    File dir = new File(path);
    File[] list = dir.listFiles();
    for (File file : list) {
        String name = file.getName();
        String[] s = name.split("\\.");
        if (s.length == 3 && type.equals(s[2])) {
            System.out.println(s[0] + "--" + s[1] + "--" + s[2]);
            file.renameTo(new File(path + "/" + s[0] + "." + s[1]));
        }
    }
}

From source file:com.hp.test.framework.Reporting.removerunlinks.java

public static void removelinksforpreRuns(String FilePath, String FileName, int lastrun)
        throws FileNotFoundException, IOException {

    log.info("Removing hyper link for the last run");
    ArrayList<String> Links_To_Remove = new ArrayList<String>();

    for (int i = 1; i < lastrun; i++) {
        Links_To_Remove.add("href=\"Run_" + i + "\\CurrentRun.html\"");
    }/*from   w w w  . ja v a2 s . c  o m*/

    File source = new File(FilePath + FileName);
    File temp_file = new File(FilePath + "temp.html");
    BufferedReader in = new BufferedReader(new FileReader(source));
    BufferedWriter out = new BufferedWriter(new FileWriter(temp_file));
    String str = "";
    while ((str = in.readLine()) != null) {

        String temp_ar[] = str.split(" ");

        for (int i = 0; i < temp_ar.length; i++) {
            String temp = temp_ar[i].trim();
            if (!temp.equals("")) {

                if (Links_To_Remove.contains(temp)) {
                    out.write(" ");
                    out.newLine();

                } else {
                    out.write(temp);
                    out.newLine();

                }

            }
        }
    }

    out.close();
    in.close();
    out = null;
    in = null;

    source.delete();
    temp_file.renameTo(source);

}