Trying to take one mp3 file and rename it using a string variable. For example, I have a classic music folder, C: / classic, and I need a song called vivaldi, renamed FourSeasons. I want to find the absolute path of the source file, C: /classical/vivaldi.mp3, then provide the string "FourSeasons.mp3" and write the file C: /classical/vivaldi.mp3 to C: /classic/FourSeasons.mp3.
I was thinking about using renameTo and writing files, but none of them gave me the desired results. The RenameTo: code returns false (failed to rename) 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 also tried using FileReader and FileWriter to create an exact copy of the file with the new name. This method outputs an mp3 file that it skips and almost never sounds like an input file. This is my file 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();
source
share