Rename folder on SD card

This should apparently be a very simple task, but I have been around it for some time without success ...

In my application, I create a folder on the SD card, where I store temporary jpg files. Since I do not want my application to show these temporary files when viewing the phone for images, I tried to hide this folder. So, right after creating the directory, I tried to rename it, for example:

String tmppath="/sdcard/myapp/tmp"; try { //this creates a directory named TMP -->OK! File f=new File(tmppath); if(!f.isDirectory()) f.mkdirs(); //this was supposed to rename the directory to .TMP, but isn't working... Process process=Runtime.getRuntime().exec("mv "+tmppath +" /sdcard/myapp/.tmp/"); process.waitFor(); } catch(SecurityException e) { } catch(IOException e) { } catch (InterruptedException e) { } 

Any thoughts?

+4
source share
3 answers
 File file = new File("your old file name"); File file2 = new File("your new file name"); boolean success = file.renameTo(file2); 
+15
source
 final File F=new File("youroldpath"); String newname="newname"; File newfile=new File(F.getParent(),newname); F.renameTo(newfile); 
+6
source

Have you tried to use the renameTo method in a file? Here is an example of renaming a file or folder.

 package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { File f = null; File f1 = null; boolean bool = false; try{ // create new File objects f = new File("C:/test.txt"); f1 = new File("C:/testABC.txt"); // rename file bool = f.renameTo(f1); // print System.out.print("File renamed? "+bool); }catch(Exception e){ // if any error occurs e.printStackTrace(); } } } 
+2
source

All Articles