0

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?

pb2q
  • 58,613
  • 19
  • 146
  • 147
Jj Delaney
  • 7
  • 1
  • 3
  • 2
    possible duplicate of [How do I trim a file extension from a String in Java?](http://stackoverflow.com/questions/941272/how-do-i-trim-a-file-extension-from-a-string-in-java) – lc. Oct 17 '12 at 15:35
  • 1
    *"the Name of the song without the .mp3 at the end of it?"* I have a collection of MP3s in which the name of the file is often not closely related to the title of the track, which is itself better expressed in the **ID3 v. 2** tags embedded in the file. – Andrew Thompson Oct 17 '12 at 15:37

3 Answers3

3

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.

pb2q
  • 58,613
  • 19
  • 146
  • 147
  • +1, this surely is more readable than the direct lastIndexOf solution. And that one dereferences the array element twice. – TheBlastOne Oct 17 '12 at 16:20
1

Use:

String fname = listOfFiles[i].getName();
fname.substring(0, fname.lastIndexOf("."))

Instead of passing..

listOfFiles[i].getName()
Reddy
  • 8,737
  • 11
  • 55
  • 73
  • That is becoming less comprehensible by the moment (mostly due to in-line code formatting). – Andrew Thompson Oct 17 '12 at 15:40
  • :) I am just continually editing the answer. I hope it is looking better now. – Reddy Oct 17 '12 at 15:44
  • See if you can live with my suggested edit, roll back if not. I think it is more clear because of the (limited amount) of color and each statement being on a single line. – Andrew Thompson Oct 17 '12 at 15:48
  • thank you Andrew, it looks much better. But how did you do it? Blockquote? – Reddy Oct 17 '12 at 15:50
  • Select a code sample and click the `{}` button. It inserts 4 spaces before each line of the selected text, and the rest is handled automagically. I often just insert the 4 spaces manually, but make sure the code 'block' has a blank line between it and preceding text (without the blank line, the formatting software gets confused). – Andrew Thompson Oct 17 '12 at 16:07
  • hmmm... I did use {} option by pressing Ctrl+k but it was wrapping line then. – Reddy Oct 17 '12 at 16:08
  • Thank you this works like a charm lastIndexOf(".") was the only difference between you and Toms answers. – Jj Delaney Oct 17 '12 at 16:09
  • *".. it was wrapping line then."* Huhh. Note that when I was 1st editing it, the last ` (from in-line code formatting) was enough to make it line wrap. Only after removing all of the acutes did it reduce to a single line (with no horizontal scroll-bar). – Andrew Thompson Oct 17 '12 at 16:15
  • will remember it. Thanks Andrew :). – Reddy Oct 17 '12 at 16:16
1
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){
         }
      }
  }
 }
Tom G
  • 3,650
  • 1
  • 20
  • 19
  • You could change listOfFiles[i].getName().indexOf(".mp3") to listOfFiles[i].getName().toLowerCase().indexOf(".mp3") – Tom G Oct 17 '12 at 15:40
  • *"You could.."* I ***would*** use the ID3 v. 2 tags, so 'no thanks'. – Andrew Thompson Oct 17 '12 at 15:42
  • You would use the ID3 v. 2 tags for what? – Tom G Oct 17 '12 at 15:44
  • For the title. See my comment on the question. – Andrew Thompson Oct 17 '12 at 15:44
  • Well yeah but that's not what he asked – Tom G Oct 17 '12 at 15:45
  • That means it reached a file without the .mp3 suffix. Are you sure every file ends with this? – Tom G Oct 17 '12 at 15:46
  • don't worry they are all lower case but i am getting a compile error >> Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(String.java:1937) at NewClass.main(NewClass.java:14) Java Result: 1 – Jj Delaney Oct 17 '12 at 15:47
  • 1
    An index of -1 means that the string ".mp3" was not found in the file. Try adding a System.out.println(listOfFiles[i].getName()); right before the writeStringToFile line – Tom G Oct 17 '12 at 15:49
  • I am getting of the songs been printed out System.out.println() but still getting the same error at the end, going to try a different way cheers – Jj Delaney Oct 17 '12 at 16:02