Reputation: 7
Only doing this because I am lazy. I just wrote a program to list all my music into an excel file.
import java.io.*;
import org.apache.commons.io.FileUtils;
public class NewClass {
public static void main( String args[] )
{
File folder = new File("C:/Users/Public/Music/OldMusic");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
try{
FileUtils.writeStringToFile(new File("test.xls"), listOfFiles[i].getName(), true);
BufferedWriter output;
output = new BufferedWriter(new FileWriter("test.xls", true));
output.newLine();
output.close();
}
catch (IOException e){
}
}
}
}
The code works fine, but at the end of every song there is .mp3 (i.e.: Metallica - Nothing Else Matters.mp3).
Is there a way just to have the Name of the song without the .mp3 at the end of it?
Upvotes: 0
Views: 420
Reputation: 59617
Since you're already using FileUtils
, note that you get methods for handling extensions.
See FileUtils.removeExtension
.
Look through the code and you'll see that these methods for handling filename extensions - also getExtension
, isExtension
- all just use lastIndexOf
, so they'll only handle the simple cases, but this should be sufficient for your purposes.
Upvotes: 3
Reputation: 8921
Use:
String fname = listOfFiles[i].getName();
fname.substring(0, fname.lastIndexOf("."))
Instead of passing..
listOfFiles[i].getName()
Upvotes: 1
Reputation: 3660
import java.io.*;
import org.apache.commons.io.FileUtils;
public class NewClass {
public static void main( String args[] )
{
File folder = new File("C:/Users/Public/Music/OldMusic");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
try{
FileUtils.writeStringToFile(new File("test.xls"), listOfFiles[i].getName().substring(0, listOfFiles[i].getName().indexOf(".mp3")), true);
BufferedWriter output;
output = new BufferedWriter(new FileWriter("test.xls", true));
output.newLine();
output.close();
}
catch (IOException e){
}
}
}
}
Upvotes: 1