Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose file in java? [duplicate]

Tags:

java

filepath

I need to get file path for my java program during runtime. Is there any way to use default dialog box to choose a single file and get its full path and name?

Its just picking a file and get its path into a String object

Can you please provide the code for it or a tutorial?

PS: Windows OS

like image 600
Hemanth S. Vaddi Avatar asked Oct 26 '16 06:10

Hemanth S. Vaddi


People also ask

How do I copy a file in Java?

If you are working on Java 7 or higher, you can use Files class copy() method to copy file in java. It uses File System providers to copy the files.

Can we compare two files in Java?

The method Files::mismatch, added in Java 12, compares the contents of two files. It returns -1L if the files are identical, and otherwise, it returns the position in bytes of the first mismatch.

How do I copy a file from one location to another?

You can move a file or folder from one folder to another by dragging it from its current location and dropping it into the destination folder, just as you would with a file on your desktop. Folder Tree: Right-click the file or folder you want, and from the menu that displays click Move or Copy.

How to work with files in Java?

In Java, with the help of File Class, we can work with files. This File Class is inside the java.io package. The File class can be used by creating an object of the class and then specifying the name of the file. Why File Handling is Required?

Is it possible to create a file chooser in Java?

Note that, in the Java look and feel at least, only directories are visible — not files. If you want to create a file chooser for a task other than opening or saving, or if you want to customize the file chooser, keep reading. We will discuss the following topics:

How do I use the file class in Java?

To use the File class, create an object of the class, and specify the filename or directory name: If you don't know what a package is, read our Java Packages Tutorial. The File class has many useful methods for creating and getting information about files. For example:

How to display a file chooser in a dialog in Java?

The class has another method, showDialog, for displaying a file chooser for a custom task in a dialog. In the Java look and feel, the only difference between this dialog and the other file chooser dialogs is the title on the dialog window and the label on the approve button.


1 Answers

Here is the example from the JFileChooser docs copy pasta with the parent sent to null.

public class PickAFile {
    public static void main(String[] args){
        JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter(
                "JPG & GIF Images", "jpg", "gif");
        chooser.setFileFilter(filter);
        int returnVal = chooser.showOpenDialog(null);
        if(returnVal == JFileChooser.APPROVE_OPTION) {
            System.out.println("You chose to open this file: " +
                    chooser.getSelectedFile().getName());
        }
    }
}

If you don't like the look of the JFileChooser try the FileDialog.

    FileDialog dialog = new FileDialog((Frame)null, "Select File to Open");
    dialog.setMode(FileDialog.LOAD);
    dialog.setVisible(true);
    String file = dialog.getFile();
    System.out.println(file + " chosen.");
like image 93
matt Avatar answered Sep 21 '22 15:09

matt