2 Replies - 1564 Views - Last Post: 17 February 2011 - 04:51 AM Rate Topic: -----

#1 man_from_man  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 31-January 11

put mp3 files into array

Posted 17 February 2011 - 03:28 AM

Hi guys.

In the program below i try to search mp3 files and put each file name into array "PlayList" and then print this array. The problems is that i can't put founded files array. Help me please.

  package org.kodejava.example.commons.io;

import org.apache.commons.io.FileUtils;
import java.io.File;
import java.util.Collection;
import java.util.Iterator;

public class SearchAndSaveMp3
{
    public static void main(String[] args)
    {
        File root = new File("/home/S08/yerzham8/Desktop");

        String PlayList[] = new String[5];
        int i = 0;
        int n = 0;

       try
       {
            String[] extensions = {"mp3"};
            boolean recursive = true;

            //
            // Finds files within a root directory and optionally its
            // subdirectories which match an array of extensions. When the
            // extensions is null all files will be returned.
            //
            // This method will returns matched file as java.io.File
            //
            Collection files = FileUtils.listFiles(root, extensions, recursive);

            for (Iterator iterator = files.iterator(); iterator.hasNext();)/>
            {
                File file = (File) iterator.next();
                PlayList[i] = file.getAbsolutePath();
                n = i++;
                System.out.println("File! = " + file.getAbsolutePath());


            }
        } catch (Exception e)
        {
            e.printStackTrace();
        }

        for (i=0; i<n+1; i++)
            System.out.println(PlayList[i]);
    }
} 


Thank you

Is This A Good Question/Topic? 0
  • +

Replies To: put mp3 files into array

#2 n8schatten  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 145
  • View blog
  • Posts: 263
  • Joined: 07-December 10

Re: put mp3 files into array

Posted 17 February 2011 - 04:44 AM

Do you get an error? How do you knot it doesn't work?
One problem I see is that you may get an ArrayOutOfBoundsExeption as soon as the spicified folder contains more than 5 mp3-files. You should consider using a list or set instead of an array. By doing so there will be several more spots to make your code less complex.
Was This Post Helpful? 0
  • +
  • -

#3 DaneAU  Icon User is offline

  • Great::Southern::Land
  • member icon

Reputation: 284
  • View blog
  • Posts: 1,617
  • Joined: 15-May 08

Re: put mp3 files into array

Posted 17 February 2011 - 04:51 AM

This ought to do it

public class Main {

  
    public static void main(String[] args) {
        ArrayList<String> music = new ArrayList<String>();
        String path = System.getProperty("user.home") + "\\Desktop\\TMP";
        System.out.println( "Searching Path\n"+path );

        File base = new File(path);
        String[] ext = {"mp3","wma"};

        Collection files = FileUtils.listFiles(base,ext,true);

        for(Iterator iter = files.iterator(); iter.hasNext(); ) {
            File f = File.class.cast( iter.next() );
            music.add( f.getPath() );
        }

        for( String s : music ) {
            System.out.println( s );
        }
    }

}


Add the files to the collection and print. Handy util that listFiles, i think i might adapt it to my meta data sorter for Media Files, would be far more efficient than my crappy method :P

Example output

Quote

Searching Path
C:\Users\Dane Leckey\Desktop\TMP
C:\Users\Dane Leckey\Desktop\TMP\01 - Maggie May.mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - Man On The Moon.mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - Microphone Fiend.mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - Nature Boy.mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - new beginnings.mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - Next Exit.mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - Nickelback - Follow You Home.mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - Not Another Drugstore (Planet Nine Mix).mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - Pennywise - Fight Till You Die - Full Circle.mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - Pete Murray - Chance to Say Goodbye.mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - Red Hot Chili Peppers - Under The Bridge.mp3
C:\Users\Dane Leckey\Desktop\TMP\01 - Rich.mp3


I trust this was helpful

This post has been edited by DaneAU: 17 February 2011 - 04:54 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1