19

How can I use JFileChooser to open two text files and after I selected these files, I want to compare them, show on the screen etc. Is this possible?

zenx
  • 271
  • 1
  • 2
  • 9

3 Answers3

42

You can have your JFileChooser select multiple files and return an array of File objects instead of one

JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.showOpenDialog(frame);
File[] files = chooser.getSelectedFiles();

The method showOpenDialog(frame) only returns once you click the ok button

EDIT

So do this:

JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.showOpenDialog(frame);
File[] files = chooser.getSelectedFiles();
if(files.length >= 2) {
    compare(readFileAsList(files[0]), readFileAsList(files[1]));
}

And change your readFileAsList to:

private static List<String> readFileAsList(File file) throws IOException {
    final List<String> ret = new ArrayList<String>();
    final BufferedReader br = new BufferedReader(new FileReader(file));
    try {
        String strLine;
        while ((strLine = br.readLine()) != null) {
            ret.add(strLine);
        }
        return ret;
    } finally {
        br.close();
    }
}
La bla bla
  • 8,260
  • 9
  • 57
  • 101
  • What you get from the file chooser is an array of Files. you can access them using compare(readFileAsList(files[0]), readFileAsList(files[1])); but you'd need to change readFileAsList to accept File instead of String – La bla bla Aug 12 '12 at 13:09
  • http://docs.oracle.com/cd/E26232_01/doc.11122/easjavaapi/com/essbase/eas/utils/TextFile.html#readFileAsList_java_io_File__java_lang_String_ If you're using Java's built in method, you don't need to do anything as it already accepts File objects But it's really difficult to help you without seeing your current code – La bla bla Aug 12 '12 at 13:13
  • Also, when you ask a question, once someone gave you the right answer here in StackOverflow, please mark it as accepted (the V under the answer) it helps if someone searches for a similar question, he can tell that this answer is correct – La bla bla Aug 12 '12 at 13:15
  • Just edit your answer to include your code, and i'll try and help you – La bla bla Aug 12 '12 at 13:27
  • la bla bla firstly thank you so much but is it neccessary that i change this part? leftList = (ArrayList) readFileAsList("C:\\Files\\file1.txt"); rightList = (ArrayList) readFileAsList("C:\\Files\\file2.txt"); – zenx Aug 12 '12 at 14:25
  • also choosing a file only I only choose file1.txt after that programme compiles, i cant choose both txt files because of error – zenx Aug 12 '12 at 14:26
  • also i cant choose any file from D driver – zenx Aug 12 '12 at 14:30
  • If you won't change leftList = .. and rightList = .. then you will just be selecting the files you write in your code. not using the JFileChooser. What error are you getting? send me an email: lablabla@gmail.com so we won't flood the comments here. I'll answer as soon as I can. – La bla bla Aug 12 '12 at 14:42
  • Also when I change my code at your comment, the compare method doesnt not compile – zenx Aug 12 '12 at 15:56
12

You can use:

JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);

// Show the dialog; wait until dialog is closed
chooser.showOpenDialog(frame);

// Retrieve the selected files.
File[] files = chooser.getSelectedFiles();

You can then use the file handles returned to do the compare.

Reimeus
  • 152,723
  • 12
  • 195
  • 261
4

In my case I solved it declaring frame as an initialized local variable set to null:

JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);

Component frame = null;

chooser.showOpenDialog(frame);
File[] files = chooser.getSelectedFiles();
Cristian Muñoz
  • 460
  • 4
  • 8