2

I am trying to prompt "open file window" from command prompt in java and planning to take that selected file as input file instead of

FileInputStream fis=new FileInputStream(args[0]);

but not succeeded yet, please some tell me how to do it in command prompt in java.

2
  • Maybe it's solution you need : stackoverflow.com/questions/4688123/… Commented Dec 1, 2016 at 15:43
  • 1
    You're looking for the JFileChooser class. FileInputStream is an inputstream, not a popup window. Commented Dec 1, 2016 at 15:46

1 Answer 1

7

You can use a JFileChooser to be able to select a file from a dialog box.

Assuming you want to launch it outside a Java Swing application, you could proceed as next:

final JFileChooser fc = new JFileChooser();
// Open the dialog using null as parent component if you are outside a
// Java Swing application otherwise provide the parent component instead
int returnVal = fc.showOpenDialog(null);
if (returnVal == JFileChooser.APPROVE_OPTION) {
    // Retrieve the selected file
    File file = fc.getSelectedFile();
    try (FileInputStream fis = new FileInputStream(file)) {
        // Do something here
    }
}

More details about How to Use File Choosers

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.