G33*_*D3R 2 java file openfiledialog command-prompt
我正在尝试从 java 中的命令提示符提示“打开文件窗口”,并计划将该选定的文件作为输入文件而不是
FileInputStream fis=new FileInputStream(args[0]);
Run Code Online (Sandbox Code Playgroud)
但还没有成功,请告诉我如何在java的命令提示符下做到这一点。
您可以使用 来JFileChooser从对话框中选择文件。
假设您想在 Java Swing 应用程序外部启动它,您可以继续执行以下操作:
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
}
}
Run Code Online (Sandbox Code Playgroud)
有关如何使用文件选择器的更多详细信息