Question

So I have a working code that is able to read the csv file but because the file is really big it takes roughly two minutes to read before all the data is displayed in an instant in the textarea. I'm using a GUI interface in eclipse with windowsbuilder. Below is the code;

JButton btnopen = new JButton("Open");
    btnopen.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent arg0) {

            try{

                final JFileChooser fc = new JFileChooser(); //launching the file chooser
                fc.setFileFilter(new FileNameExtensionFilter("Text Files", "txt")); //this will allow text files to be read
                fc.setFileFilter(new FileNameExtensionFilter("CSV", "csv")); //this will allow csv files to be read
                fc.setFileFilter(new FileNameExtensionFilter("JSON", "json")); //this will allow json files to be read
                fc.setFileFilter(new FileNameExtensionFilter("XML", "xml")); //this will allow xml files to be read

                int returnVal = fc.showOpenDialog(contentPane);
                File f; //file that holds the data from the text file
                fc.removeChoosableFileFilter(fc.getAcceptAllFileFilter());
                if(returnVal == JFileChooser.APPROVE_OPTION) {
                    f = fc.getSelectedFile(); //tells file chooser to get the file selected and store into file variable
                    String output="";
                    //use buffered reader and file reader to read selected file
                    BufferedReader in = new BufferedReader(new FileReader(f));
                    //after reading data, store in to string
                    String line = in.readLine(); //every time a line is read, data is put into text area
                    int i=0;
                    while(line!=null){ //while still reading...
                        //
                        line = in.readLine(); //continue reading next line of file
                        output +=line+"\n";
                        //textArea.append(line +"\n"); //add text from file into text area
                        //++i;
                    }

                    textArea.append(output);
                }
            }
        catch(Exception e){

        }

        }           
    });
Was it helpful?

Solution

@HectorLector's response will optimize the reading a bit, but they're right; utltimately, reading a file is going to take as long as it takes. I suspect that your underlying question might be "how can I make my UI responsive while I'm reading this file?" - right now, since you're doing the reading inside an ActionListener, your UI will be completely blocked while the file is read, which makes for a terrible user experience.

The answer to this is that long-running operations like filesystem access should be done on a background thread. Consider using a SwingWorker (see the tutorial) for a task like this; the worker thread can build the string and use the done() method to update the text area (or, if you want to show the file as it's being read, use a combination of the publish() and process() methods).

Also, as a side note, be sure you're close()ing that BufferedReader you're using to read the file. Wrap the reading itself in a try block, and close inside finally in case there are any problems during reading. Something like this:

BufferedReader in = new BufferedReader(new FileReader(f));
StringBuilder output = new StringBuilder();
try {
    while (in.hasNext()) {
        output.append(in.readLine());
        output.append("\n");
    }
finally {
    in.close();
}

OTHER TIPS

Use a StringBuilder to create your string. Because strings are immutable a new object is created every time when you use "+" to append something.
This will increase performance but may not be the main problem. Filesystem access is slow. Maybe try reading your file one time and keep it in memory (in a list or something). Or try only showing parts of the file at a time (paging).

                BufferedReader in = new BufferedReader(new FileReader(f));
                StringBuilder builder = new StringBuilder();
                String line = "";

                while((line = in.readLine()) != null){ 
                     builder.append(line);
                     builder.append("\n");
                } 

                textArea.append(builder.toString());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top