Roger Travis
Roger Travis

Reputation: 8538

How to rename a file?

A quick question, how do i rename a file?

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

tried like that, but doesn't seem to work.

Thanks!

Upvotes: 0

Views: 330

Answers (3)

ZZnOB
ZZnOB

Reputation: 86

    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

Upvotes: 2

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21191

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");
}

This worked for me. please check once!!

Upvotes: 1

ekholm
ekholm

Reputation: 2573

I think getAbsolutePath() returns the full path including file name, that might be a problem here. Try getParent() instead and see if it works.

Upvotes: 1

Related Questions