Choose a file

Solved
Vitaldix Posted messages 116 Registration date   Status Membre Last intervention   -  
Vitaldix Posted messages 116 Registration date   Status Membre 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:
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 réponse

KX Posted messages 19031 Status Modérateur 3 020
 
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
1
Vitaldix Posted messages 116 Registration date   Status Membre Last intervention   6
 
I can't process with a Scanner!
Scanner sc = new Scanner(files);
doesn't work.
0
KX Posted messages 19031 Status Modérateur 3 020
 
It's because "files" is an array, not a file!
It contains all the selected files, so you need to process the files one by one with your Scanner.

File[] files = chooser.getSelectedFiles(); for (File file : files) { Scanner sc = new Scanner(file); ... sc.close(); }
0
Vitaldix Posted messages 116 Registration date   Status Membre Last intervention   6
 
Ah, yes! But basically, I only want to select a single file; wouldn't that be simpler in that case?
0
KX Posted messages 19031 Status Modérateur 3 020
 
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(); }
0
Vitaldix Posted messages 116 Registration date   Status Membre Last intervention   6
 
Thank you, it's working!
However, I have an issue with the execution of the program, now I have
Exception 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?
0