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?
JFileChooser to open multiple txt files
Asked Answered
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();
}
}
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 –
Stockholder
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 code –
Stockholder
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 –
Stockholder
Just edit your answer to include your code, and i'll try and help you –
Stockholder
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 error –
Pepsinate
also i cant choose any file from D driver –
Pepsinate
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 compile –
Pepsinate
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.
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();
© 2022 - 2024 — McMap. All rights reserved.