ColonDelimitedMovieFinder Class – Java

package moviefindertest.springxml;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class ColonDelimitedMovieFinder implements MovieFinder {

      public List<Movie> findAll() {
            List<Movie> movieList = new ArrayList<Movie>();
            File file = new File(".\\bin\\Movies.txt");
            boolean fileExists = file.exists();
            if(fileExists) {
                  try {
                        Scanner scanner = new Scanner(file);
                        scanner.useDelimiter(":|\r\n");  // Split on colon or end of line
                        while(scanner.hasNext()) {
                              String title = scanner.next();
                              String director = scanner.next();
                              movieList.add(new Movie(title, director));
                        }
                  }
                  catch(FileNotFoundException e){}
            }
       

            return movieList;
      }

}
 
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s