Choose a file
Solved
Vitaldix
Posted messages
116
Registration date
Status
Member
Last intervention
-
Vitaldix Posted messages 116 Registration date Status Member Last intervention -
Vitaldix Posted messages 116 Registration date Status Member Last intervention -
Hello!
So here is my little problem! I would like my program to ask the user to choose a file; the user can choose any file they want, and the program retrieves the content of that file.
I have this:
which works wonderfully but of course it only works for the file located in that spot.
I also found this:
There I can indeed navigate through my directories and select any file I want, but nothing happens! I feel like I can't retrieve my file!
I tried to tinker with things, mixing these two pieces of code together, but nothing works; can you help me out?
Thanks!
So here is my little problem! I would like my program to ask the user to choose a file; the user can choose any file they want, and the program retrieves the content of that file.
I have this:
File file = new File("file_location"); Scanner sc = new Scanner(file); which works wonderfully but of course it only works for the file located in that spot.
I also found this:
FileSystemView systemView = FileSystemView.getFileSystemView(); //retrieving directories File defaultDir = systemView.getDefaultDirectory(); File home = systemView.getHomeDirectory(); //creating and displaying JFileChooser JFileChooser defaultChooser = new JFileChooser(defaultDir); defaultChooser.showOpenDialog(null); JFileChooser homeChooser = new JFileChooser(home); homeChooser.showOpenDialog(null);
There I can indeed navigate through my directories and select any file I want, but nothing happens! I feel like I can't retrieve my file!
I tried to tinker with things, mixing these two pieces of code together, but nothing works; can you help me out?
Thanks!
1 answer
-
JFileChoosers only allow the selection of one (or multiple) files (and/or directories) for reading (or writing), but once the files are chosen, it's up to you to process them.
See: How to Use File Choosers
JFileChooser chooser = new JFileChooser(); chooser.setFileSelectionMode(JFileChooser.FILES_ONLY); if (chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) { File[] files = chooser.getSelectedFiles(); ... }
--
Trust does not exclude control-
-
-
-
There is a shortcut with the getSelectedFile method that returns a single file, but you need to make sure that you can only choose one, because if you choose multiple but only one is kept, there will be a problem...
chooser.setMultiSelectionEnabled(false); if (chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) { File file = chooser.getSelectedFile(); Scanner sc = new Scanner(file); ... sc.close(); } -
Thank you, it's working!
However, I have an issue with the execution of the program, now I haveException while removing reference: java.lang.InterruptedException java.lang.InterruptedException at java.lang.Object.wait(Native Method) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at java.lang.ref.ReferenceQueue.remove(Unknown Source) at sun.java2d.Disposer.run(Unknown Source) at java.lang.Thread.run(Unknown Source)
do you know where it comes from?
-