1

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

}

}

Mebin Joe
  • 1,806
  • 3
  • 13
  • 21
  • Have you tried adding the filter for html? You already have txt and csv ... – Fildor Nov 12 '15 at 12:03
  • @Fildor yes. but how to get the file contents in java..? – Mebin Joe Nov 12 '15 at 12:05
  • Maybe this link will help you: https://docs.oracle.com/javase/tutorial/essential/io/ Explaining the same in an answer would really be inefficient. – Fildor Nov 12 '15 at 12:08
  • Possible duplicate of [Best way to read a text file in Java?](http://stackoverflow.com/questions/4716503/best-way-to-read-a-text-file-in-java) – Fildor Nov 12 '15 at 12:10
  • @Fildor No. in that case the file is specified..but i need to select the file manually by choosing from a particular directory – Mebin Joe Nov 12 '15 at 12:21
  • But you already have the file ... `chooser.getSelectedFile()` – Fildor Nov 12 '15 at 12:22
  • @Fildor file is choosen at runtime by using Swing Component JFileChooser.. My question is can I do the same with html .. Get the file name and context into a buffer and process it by readLine(). But how to get the file in java from html ? – Mebin Joe Nov 12 '15 at 12:42
  • Sorry, I do not understand what you are trying to do. – Fildor Nov 12 '15 at 12:44

0 Answers0