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?
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();
}
}
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();
Related
I'm doing some coding for an assignment. I'm using java swing to do it. I need to know that how should change my code to display multiple files content in a text area.
I've tried some codes. I added one jButton and one jTextArea to read multiple files. I already know something about setMultiSelectionEnabled(true) and getSelectedFiles().
//This is my code inside the jButton
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.showOpenDialog(null);
File files = chooser.getSelectedFiles();
String filename = files.getAbsolutePath();
try{
FileReader reader = new FileReader(filename);
BufferedReader br = new BufferedReader(reader);
jTextArea1.read(br, null);
br.close();
jTextArea1.requestFocus();
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
I can only get one file content into my text area yet. Please help me to develop this. Thank you!
If you want multiple files in a single text area, then you can't use the read(...) method.
Instead you will need to read each file line by line and the add the text to the text area using the append(...) method.
try this code:
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(true);
chooser.showOpenDialog(null);
File[] files = chooser.getSelectedFiles();
// String filename = files.getAbsolutePath();
for(File f:files){
FileReader reader = new FileReader(f);
BufferedReader br = new BufferedReader(reader);
while( (line = br.readLine()) != null ) {
jTextArea1.append(line);
}
br.close();
//jTextArea1.requestFocus();
}
The following JFileChooser code works fine, except that the FileFilter doesn't filter. It doesn't do anything. From another stackoverflow answer: "Filename filters do not function in Sun's reference implementation for Microsoft Windows."
Comment from Nov 21st, 2016
Is there a FileFilter workaround for Windows?
public String getPathFileName(String startingDir) {
String returnSelectedFile = "";
JFileChooser fileChooser = new JFileChooser(startingDir);
FileFilter filter = new FileNameExtensionFilter("Excel file", "xls", "xlsx");
fileChooser.addChoosableFileFilter(filter);
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
returnSelectedFile = selectedFile.getPath();
}
return returnSelectedFile;
}
I have found this to work:
final JFileChooser chooser = new JFileChooser();
chooser.setFileFilter(new FileNameExtensionFilter("CSV FILES", "csv"));
I have found that this works for one file filter, but I cannot confirm for multiple file filters. Hope this helps.
I'm trying to use JFileChooser to select a folder. It returns the parent folder, not the current folder.
Example I select folder documents, then backup. JFileChooser returns documents instad of backup.
String getFilePath()
{
JFileChooser fc = new JFileChooser();
// fc.setCurrentDirectory(new File(System.getProperty("user.home")));
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
fc.setAcceptAllFileFilterUsed(false);
int returnVal = fc.showOpenDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File rt=fc.getCurrentDirectory();
String t=rt.getName();
return t;
}
return null;
}
The issue is that you are using JFileChooser.getCurrentDirectory() instead of JFileChooser.getSelectedFile().
export content to an xls file using java with save dialog:
- Where should I ask for a file name during save?
I prefer to ask what to name the file when it is saved.
public static void main(String args[])
{
SaveToExcel exp = new SaveToExcel();
exp.writePropertiesIntoExcel("D:\\ExcelFile.xls");
}
You should use JFileChooser if you want to let the users choose where to save the file.
JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.APPROVE_OPTION);
chooser.setMultiSelectionEnabled(false);
int returnVal = chooser.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION){
File f = chooser.getSelectedFile();
String a = f.getAbsolutePath();
SaveToExcel exp = new SaveToExcel();
exp.writePropertiesIntoExcel(a);
}
A simple code you can use in your method, this gets the path you want to save your file in.
while i am trying to save the file its not working but for making folders it works. what should i do ? i am new in java also. plz help
public void actionPerformed(ActionEvent ae)
{
if(ae.getSource()==save)
{
JFrame parentFrame = new JFrame();
JFileChooser fileChooser = new JFileChooser();
fileChooser.setDialogTitle("Specify a file to save");
int userSelection = fileChooser.showSaveDialog(parentFrame);
if (userSelection == JFileChooser.APPROVE_OPTION)
{
File fileToSave = fileChooser.getSelectedFile();
System.out.println("Save as file: " + fileToSave.getAbsolutePath());
}
}
}
You choose a file but don't create it write anything to it. The file will not be created until you actually create it or write something to it, for example with
FileWriter writer = new FileWriter(fileToSave);
writer.write("Hello!");
writer.close();
First, get the file you want to save as a File.
Then, write it to a new directory using BufferedWriter to a new directory.
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(aComponent); //parent component to JFileChooser
if (returnVal == JFileChooser.APPROVE_OPTION) { //OK button pressed by user
File file = fc.getSelectedFile(); //get File selected by user
o = new BufferedWriter(new FileWriter(file)); //use its name
//write things here
o.flush();
o.close();
}
Look at How to save file using JFileChooser in Java? and How to save a txt file using JFileChooser?