一个简单的问题,如何重命名文件?
File to = new File(f.getAbsolutePath(), etRenameStr.getText().toString() );
f.renameTo(to);
expl();
尝试了这样,但似乎没有用。
谢谢!
答案 0 :(得分:2)
File dir = Environment.getExternalStorageDirectory();
if(dir.exist()){
File from = new File(dir,"from.mp4");
File to = new File(dir,"to.mp4");
if(from.exist())
from.renameTo(to);
}
http://developer.android.com/reference/java/io/File.html#renameTo%28java.io.File%29
答案 1 :(得分:1)
我认为getAbsolutePath()
会返回包含文件名的完整路径,这可能是一个问题。请尝试getParent()
,看看它是否有效。
答案 2 :(得分:1)
File rootDir = Environment.getExternalStorageDirectory();
File file = new File(rootDir + "/Files/"+fileName);
File file2 = new File("newname");
// Rename file (or directory)
boolean success = file.renameTo(file2);
if (!success) {
System.out.println("File was not successfully renamed");
}
这对我有用。请检查一次!!