Reputation: 723
I'm creating a personal movie database thingy and i want to populate a combo box with movie titles from IMDB, IMDB releases this information in text files, so i'm trying to populate it from those text files. Ive got it working, but since the text file is VERY large, almost 80 000 rows with a title on every row... it takes way to long to load.
This might be the wrong way to go about doing this, someone knows how to solve it or what I should do?
The code for reading the file and return the String [] for the combo box
public String [] getMoviesFromFile() throws IOException{
BufferedReader input = new BufferedReader(new FileReader(filePath));
try {
String line = null;
while (( line = input.readLine()) != null){
strings.add(line);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally {
input.close();
}
String[] lineArray = strings.toArray(new String[]{});
return lineArray;
}
Upvotes: 1
Views: 245
Reputation: 347334
The problem your having is your blocking the Event Dispatching Thread, which will make your application come to a grinding halt while the file is begin read. You should never perform time consuming or blocking actions in the EDT.
You need to off load the loading to a background thread and load the list there, then re-sync the values back to the EDT (you should never create or modify any UI element out side of the EDT)
Have a look at Concurrency in Swing. In your case, I'd recommend taking a look at SwingWorker as it's designed to meet your actual requirements.
File I/O may be to slow for your needs, I might suggest you look at loading the text file into a SQL style database, which may give faster results.
I'd suggest looking at HyperSQL or H2 which are both pure Java SQL databases designed to be small and lightweight, but which also run in single user mode, meaning you don't need to install a fully fledged SQL server in order to use them
Upvotes: 2