rename a file - Android File Input Output

Android examples for File Input Output:File Rename

Description

rename a file

Demo Code


//package com.java2s;

import java.io.File;

public class Main {

    public static void rename(String from, String to, boolean delete) {
        rename(new File(from), new File(to), delete);
    }// w  w  w .j  a va2  s  . c  om

    public static void rename(File from, File to, boolean delete) {
        if (!from.exists()) {
            return;
        }
        boolean isRename = false;
        if (to.exists()) {
            if (delete) {
                to.delete();
                isRename = true;
            } else {
                isRename = false;
            }
        } else {
            isRename = true;
        }
        if (isRename) {
            File parent = to.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            from.renameTo(to);
        }
    }
}

Related Tutorials