All pastes #1576713 Raw Edit

Directory Walkthrough

public text v1 · immutable
#1576713 ·published 2009-09-23 17:08 UTC
rendered paste body
package playlists;

import java.io.File;



/**
 * @author wangruowen
 * @modified by Andrew Hall
 */
public class DirectoryWalkThrough {
	
	/**
	 * 
	 */
	File dirFile;
	
	/**
	 * 
	 */
	File[] fileArray;

	/**
	 * Construct a instance with the 
	 * @param directoryPath
	 */
	public DirectoryWalkThrough(String directoryPath) {
		// TODO Auto-generated constructor stub
		dirFile = new File(directoryPath);
		if (!dirFile.isDirectory()) {
			System.err.println("Error: " + directoryPath + "is not a directory!");
			System.out.println("Please choose another directory path.");
		}
	}
	
	/**
	 * walkthrough the specified directory and return all its files in it.
	 * @return File[] - the files contained in this directory
	 */
	public File[] walkThrough() {
		return dirFile.listFiles();
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		DirectoryWalkThrough mydwt = new DirectoryWalkThrough("C:\\");
		
		Playlist[] playList = new Playlist[40]; // Will break if more than 40 playlists.... but yea
		
		//Counter for the playList array
		int count =0;
		
		// All the arrays for songs, artists, genres
		CSC216ArrayList<Song> songArray = null;
		CSC216ArrayList<Artist> artistArray = null;
		CSC216ArrayList<Genre> genreArray = null;
		
		for (File eachFile : mydwt.walkThrough()) {
			if(eachFile.toString().endsWith("xml")){// Make sure it is an xml file
				iTunesParser parse = new iTunesParser(eachFile.toString()); // make the itunes parser
				Song temp = parse.readSong();
				
				songArray = new CSC216ArrayList<Song>(); // array for holding songs
				artistArray = new CSC216ArrayList<Artist>(); // array for holding artist
				genreArray = new CSC216ArrayList<Genre>(); // array for holding genre
				
				while(temp != null){
					
					songArray.add(temp);
					
					if(temp.getArtist() != null){
						Artist tempart = new Artist(temp.getArtist(),temp.getPlayCount(), temp.getOccurrences());
						artistArray.add(tempart);
					}
					else{
						Artist tempart = new Artist("NONE",0,0);
						artistArray.add(tempart);
					}
					if(temp.getGenre() != null){
						Genre tempg = new Genre(temp.getGenre(),temp.getPlayCount(), temp.getOccurrences());
						genreArray.add(tempg);
					}
					else{
						Genre tempg = new Genre("NONE",0,0);
						genreArray.add(tempg);
					}
					
					temp = parse.readSong();
				}
				// make a new playlist and add it to the playlist array
				if(songArray != null){
					System.out.println("Make new plalyist "+eachFile.getName()+" with "+songArray.size()+" songs");
					playList[count] = new Playlist(songArray,artistArray,genreArray,eachFile.getName());
					count++;
				}
			}
		}
	}
}