一个简单的问题,我如何重命名文件?
File to = new File(f.getAbsolutePath(), etRenameStr.getText().toString() );
f.renameTo(to);
expl();
像那样尝试过,但似乎不起作用。
谢谢!
一个简单的问题,我如何重命名文件?
File to = new File(f.getAbsolutePath(), etRenameStr.getText().toString() );
f.renameTo(to);
expl();
像那样尝试过,但似乎不起作用。
谢谢!
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
我认为getAbsolutePath()
返回包含文件名的完整路径,这可能是一个问题。试试看getParent()
它是否有效。
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");
}
这对我有用。请检查一次!!