Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to rename a file in android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.60k
    Comment on it

    Below is the code to rename a file, in this code newdirectory is the path where the file is existing . The Boolean done will give true value if the file get renamed.

    String PIC_FILE_NAME="hello.png";
    String UPDATED_PIC_FILE="world.png";
    
    File directory = new File(Environment.getExternalStorageDirectory(), getString(R.string.app_name));
    
    File newdirectory = new File(directory, sharedPreferenceManager.getOperatorName(getActivity()));
    File pic=null;
    Filempic=null;
    boolean done=false;
    
    try
        {
    
     pic=new File(newdirectory,PIC_FILE_NAME);
    //File from=new File(path,file name);
    
     mpic=new File(newdirectory,UPDATED_PIC_FILE);
    //File to=new File(path,updated file name);
    
    done=pic.renameTo(mpic);      //Renaming file
    
    System.out.println("File has been renamed "+done);
    //above line will print done value true if file has been renamed else it will print false value
    
    }catch(Exception e){
    e.printStackTrace();
    //if any error occure
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: