JFileChooser to open multiple txt files
Asked Answered
P

3

21

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?

Pepsinate answered 12/8, 2012 at 12:27 Comment(0)
S
47

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();
    }
}
Stockholder answered 12/8, 2012 at 12:30 Comment(9)
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 StringStockholder
docs.oracle.com/cd/E26232_01/doc.11122/easjavaapi/com/essbase/… 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 codeStockholder
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 correctStockholder
Just edit your answer to include your code, and i'll try and help youStockholder
la bla bla firstly thank you so much but is it neccessary that i change this part? leftList = (ArrayList<String>) readFileAsList("C:\\Files\\file1.txt"); rightList = (ArrayList<String>) readFileAsList("C:\\Files\\file2.txt");Pepsinate
also choosing a file only I only choose file1.txt after that programme compiles, i cant choose both txt files because of errorPepsinate
also i cant choose any file from D driverPepsinate
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: [email protected] so we won't flood the comments here. I'll answer as soon as I can.Stockholder
Also when I change my code at your comment, the compare method doesnt not compilePepsinate
A
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.

Agitate answered 12/8, 2012 at 12:31 Comment(0)
B
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();
Blasphemous answered 19/4, 2016 at 11:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.