I have tried with JFileChooser..Is there a possiblity to do the same with html file tag input type="file" ?
Please help...!
Any other substitute for JFileChooser..?
I need to manually choose the file from a specific location and not to specifically hardcode the file in the program
Can I trigger a button to call the input file and get its contents using readLine()
public class TestPane extends JPanel {
private static final long serialVersionUID = 1L;
private JButton open;
private JFileChooser chooser;
public TestPane() {
setLayout(new BorderLayout());
open = new JButton("Open");
add(open, BorderLayout.SOUTH);
open.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (chooser == null) {
chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
chooser.addChoosableFileFilter(new FileFilter() {
@Override
public boolean accept(File f) {
return f.isDirectory() || f.getName().toLowerCase().endsWith(".txt") || f.getName().toLowerCase().endsWith(".csv");
}
@Override
public String getDescription() {
return ("Text Files (*.txt) or CSV Files (*.csv)");
}
});
}
switch (chooser.showOpenDialog(TestPane.this)) {
case JFileChooser.APPROVE_OPTION:
try (BufferedReader br = new BufferedReader(new FileReader(chooser.getSelectedFile()))) {
String text = null;
while ((text = br.readLine()) != null) {
service.addUserFromFile(text);
}
} catch (IOException exp) {
exp.printStackTrace();
JOptionPane.showMessageDialog(TestPane.this, "Failed to read file", "Error", JOptionPane.ERROR_MESSAGE);
}
break;
}
}
});
}
}
}