How to Choose File in Java


In this example, let’s  see how to select a file in Java.

Source Code

package com.beginner.examples;

import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileSystemView;

public class FileChooserExample {

    public static void main(String[] args) {

    	JFileChooser jfc = new JFileChooser(FileSystemView.getFileSystemView().getHomeDirectory());
		int v = jfc.showOpenDialog(null);

		if (v == JFileChooser.APPROVE_OPTION) 
		{
			File file = jfc.getSelectedFile();
			System.out.println(file.getAbsolutePath());
		}
    }
}

Output:

C:UsersPublicDesktopFoxmail.lnk

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments