JFileChooser to open multiple txt files


https://docs.oracle.com/javase/7/docs/api/javax/swing/JFileChooser.html

MULTI_SELECTION_ENABLED_CHANGED_PROPERTY

public static final String MULTI_SELECTION_ENABLED_CHANGED_PROPERTY

Enables multiple-file selections.

See Also:

Constant Field Values

==============================================================

http://stackoverflow.com/questions/11922152/jfilechooser-to-open-multiple-txt-files


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();
    }
}


你可能感兴趣的:(JFileChooser to open multiple txt files)