0

一个简单的问题,我如何重命名文件?

File to  = new File(f.getAbsolutePath(), etRenameStr.getText().toString() );
f.renameTo(to);
expl();

像那样尝试过,但似乎不起作用。

谢谢!

4

3 回答 3

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

于 2012-08-10T11:54:30.940 回答
1

我认为getAbsolutePath()返回包含文件名的完整路径,这可能是一个问题。试试看getParent()它是否有效。

于 2012-08-10T11:52:34.713 回答
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");
}

这对我有用。请检查一次!!

于 2012-08-10T11:52:49.210 回答