JavaFX文件选择器新建文件

4

是否可以使用JavaFX文件选择器(或类似的替代方案)创建新文件?

在Linux上(确切地说是Ubuntu),输入不存在的文件名是可行的,但在Windows上,文件选择器不允许这样做。

1个回答

6
是的,这是可能的,您只需要知道要调用的正确函数。 FileChooser 的 API 在其开篇段落这里详细说明了它们。

FileChooser 可用于调用文件打开对话框以选择单个文件(showOpenDialog),调用文件打开对话框以选择多个文件(showOpenMultipleDialog)和调用文件保存对话框(showSaveDialog)。

用法

保存文件:

    FileChooser fileChooser = new FileChooser();
    File selectedFile = fileChooser.showSaveDialog(null);

打开一个文件:

    FileChooser fileChooser = new FileChooser();
    File selectedFile = fileChooser.showOpenDialog(null);

打开多个文件:
    FileChooser fileChooser = new FileChooser();
    List<File> files = fileChooser.showOpenMultipleDialog(null); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接