3

Trying to take one mp3 file and rename it using a string variable. For example, I have a classical music folder, C:/classical, and I want a song called vivaldi renamed to FourSeasons. I want to find the absolute path of the initial file, C:/classical/vivaldi.mp3, then provide a string, "FourSeasons.mp3", and have the file C:/classical/vivaldi.mp3 changed to C:/classical/FourSeasons.mp3.

I've thought of using renameTo and file writer, but neither of these have given me desired results. RenameTo code : this returns false (rename failed), and tends to permanently delete my file.

public static void main(String[] args) {
File mp3 = new File("C:/mp3.mp3");
boolean renamestatus = mp3.renameTo(new File("song.mp3"));
System.out.println(renamestatus);
}

I've also tried to use FileReader and FileWriter to make an exact copy of the file, with a new name. This method outputs an mp3 file that skips and is nowhere near sounding like the input file This is my fileWriter code:

File inputFile = new File("C:/mp3.mp3");
File outputFile = new File("C:/song.mp3");
FileReader in = new FileReader(inputFile);
FileWriter out = new FileWriter(outputFile);
int c;
while ((c = in.read()) != -1)
  out.write(c);
in.close();
out.close();
  • 1
    Can you reproduce the problem of `renameTo()` deleting your file? Are you **sure** that it doesn't just move it somewhere where you don't expect it? Because **if** it does, then that's **definitely** a bug! – Joachim Sauer Jun 01 '11 at 07:59
  • It could move it to a system folder or somewhere on the $CLASSPATH or $PATH, do a search for it to be sure, as Joachim said its very unlikely its deleted. – Simeon Jun 01 '11 at 08:04
  • Why on earth should the file be moved to "a system folder or somewhere on the $CLASSPATH or $PATH"? File paths are relative to the current working directory. – jarnbjo Jun 01 '11 at 08:15
  • @Joachim: If renameTo returns false, it should not even move the file somewhere unexpected. – jarnbjo Jun 01 '11 at 08:16
  • @jarnbjo: that's true as well. So can you reliably reproduce it? If so, then you should probably report this as a bug. – Joachim Sauer Jun 01 '11 at 08:38

5 Answers5

5

I also faced same problem,

I solved it using Absolute path of the new file object.

So in your case :

boolean renamestatus = mp3.renameTo(new File("song.mp3"));

should be

boolean renamestatus = mp3.renameTo(new File("C://song.mp3"));

Secondly, without using Absolute path, your file doesn't get deleted, but it is moved to your project's root folder with new name.

Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
2

When using renameTo you need to specify the path that you want the file to go to.For example with the one you are using the original path is "C:/mp3.mp3" you would want the path in renameTo to look like "C:/song.mp3". So your line would look something like this.

boolean renamestatus = mp3.renameTo(new File("C:/song.mp3"));

Hope that is helpful for you.

CF711
  • 301
  • 1
  • 5
  • 14
1

Try like this:

mp3.renameTo(new File("C:/song.mp3"));

You can also have a look at this and this answers.

Community
  • 1
  • 1
Simeon
  • 7,582
  • 15
  • 64
  • 101
  • It shouldn't change anything to the main problem. Why when the renameTo method is called and returns false the original file is deleted ? – Colin Hebert Jun 01 '11 at 08:05
  • @Colin It should not be deleted, its probably just moved to a default place. Also creating a file on relative path creates it on the $PATH somewhere. I will have time to test it in about 30min. As to returning false however, this is odd, but more info is needed to be able to find out why it happens I think. – Simeon Jun 01 '11 at 08:09
1

FileReader and FileWriter are made to handle textual data only.

Your MP3 files are binary data and should be treated as such, so you'd need to use FileInputStream and FileOutputStream for reading/writing.

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
0

Its not deleting original, Its working fine, Just give complete path(Source file path + new name)

Prashant Vhasure
  • 916
  • 2
  • 7
  • 18