Reputation: 157
This must be, supposedly, a very simple task, but I've been around it for some time without successes...
In my app I create a folder in the SD card, where I store temporary jpg files. Since I don't want my app to show those temp files when browsing the phone for images, I was trying to make that folder hidden. So, right after creation the dir, I tried to rename it, like this:
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?
Upvotes: 4
Views: 9769
Reputation: 4719
Have you tried using the renameTo method in 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();
}
}
}
Upvotes: 2
Reputation: 61
final File F=new File("youroldpath");
String newname="newname";
File newfile=new File(F.getParent(),newname);
F.renameTo(newfile);
Upvotes: 6
Reputation: 548
File file = new File("your old file name");
File file2 = new File("your new file name");
boolean success = file.renameTo(file2);
Upvotes: 15