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)