rename a file PROPERLY on disk

Im trying to rename a file.. I know you're just going to tell me to use 'renameTo' but that doesnt actually change the files name on disk, it just changes to name of a 'File' object.

How do I actually change the name of a physical file on disk that can be seen in a file browser or windows explorer? PERL can do this in one line :P

[349 byte] By [Andy_Ja] at [2007-11-27 11:12:04]
# 1

> Im trying to rename a file.. I know you're just going

> to tell me to use 'renameTo' but that doesnt actually

> change the files name on disk, it just changes to

> name of a 'File' object.

>

says who?

Quote from API:

Renames the file denoted by this abstract pathname.

masijade.a at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 2

> ... PERL can do this in one line :P

Java too.

prometheuzza at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 3

nice, i like the helpful replies..

my point is that renameTo didnt rename my files.. maybe i'll test it in a small program.

Thanks for nothing :)

Andy_Ja at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 4

> nice, i like the helpful replies..

> my point is that renameTo didnt rename my files..

> maybe i'll test it in a small program.

Your point and the thing you posted are two different things. In your original post you said that renameTo(...) didn't rename files, which is incorrect. The fact that it didn't rename YOUR file is something entirely different! You most probably did something wrong like not closing a stream, or your OS is locking the file.

> Thanks for nothing :)

?

prometheuzza at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 5

Are you still keeping a lock on that file somehow, e.g. by not closing a stream? It can't rename a file if you don't let it.

CeciNEstPasUnProgrammeura at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 6

well im using Ubuntu Linux and im sure I have permission to alter the files.. Id appreciate it If somebody could show me how to rename files by either linking me to a site or posting some code.. I found this example:

// File (or directory) with old name

File file = new File("oldname");

// File (or directory) with new name

File file2 = new File("newname");

// Rename file (or directory)

boolean success = file.renameTo(file2);

if (!success) {

// File was not successfully renamed

}

can somebody explain it to me? does this actually alter files on disk, or just objects you have created?

if I did the following, would it actually rename the file i have on the disk, or just the variable 'file'?

// File (or directory) with old name

File file = new File("C\:\\music\\oldsongname\.mp3");

// File (or directory) with new name

File file2 = new File("C\:\\music\\newsongname\.mp3");

// Rename file (or directory)

boolean success = file.renameTo(file2);

if (!success) {

// File was not successfully renamed

}

sorry if my backslashes are in the wrong places, but you can see what i mean

Andy_Ja at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 7

As already stated:

- the file is really renamed on disk;

- if the returned value is false, then there is probably still a process that is locking the file (unclosed stream?).

prometheuzza at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 8

import java.io.File;

public class MainClass {

/**

* @param args

*/

public static void main(String[] args) {

// File (or directory) with old name

File file = new File("/home/andy/testfile.txt");

// File (or directory) with new name

File file2 = new File("/home/andy/tesfilewithnewname.txt");

// Rename file (or directory)

boolean success = file.renameTo(file2);

if (!success) {

System.out.println("Error");

}else{

System.out.println("Done");

}

}

}

That works! must be something to do with my code.. my program is too complicated to post here but i know what the problem is now

Andy_Ja at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 9

> That works! must be something to do with my code.. my

> program is too complicated to post here but i know

> what the problem is now

9 out of 10 times it's an unclosed stream. You are probably reading/writing to the file in your code, right?

prometheuzza at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 10

> // File (or directory) with old name

> File file = new

> File("C\:\\music\\oldsongname\.mp3");

>

> // File (or directory) with new name

> File file2 = new

> File("C\:\\music\\newsongname\.mp3");

Are you sure these file names are correct (eg. file.exists() == true?)?

I think, these are neither valid Unix nor Windows file names.

On Windows it would probably be something like:

"C:\\music\\oldsongname.mp3"

On Unix: there's nothing like "C:"

-Puce

Message was edited by:

Puce

Pucea at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 11

puce that was just an example.. its not important..

its not an open stream i dont think, im not actually writing to anything, im just renaming files.. im pretty sure its to do with me not being careful about my file names.. ie i think im passing it:

oldsong.mp3

rather than:

c:\music\oldsong.mp3

to keep with m original example.

I dont have the time right now but i just need to be sure of whats going on

Andy_Ja at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...
# 12

Debug it (using a debugger from an IDE such as NetBeans or Eclipse).

Also add a watch to see if your orignial file really exists.

-Puce

Pucea at 2007-7-29 13:52:14 > top of Java-index,Java Essentials,Java Programming...