例1:点击按钮Choose File打开文件选择器,并打开指定的目录。这是通过final void setInitialDirectory(final File value)方法实现的。

 1 import java.io.File;
 2
 3 import javafx.application.Application;
 4 import javafx.event.ActionEvent;
 5 import javafx.event.EventHandler;
 6 import javafx.geometry.Insets;
 7 import javafx.geometry.Pos;
 8 import javafx.scene.Scene;
 9 import javafx.scene.control.Button;
10 import javafx.scene.layout.GridPane;
11 import javafx.stage.FileChooser;
12 import javafx.stage.FileChooser.ExtensionFilter;
13 import javafx.stage.Stage;
14
15 public class Main extends Application {
16
17     public static void main(String[] args) {
18         launch(args);
19     }
20
21     @Override
22     public void start(Stage primaryStage) throws Exception {
23         // Create a pane to hold a button
24         GridPane pane = new GridPane();
25         pane.setStyle("-fx-border-color: green;");
26         pane.setAlignment(Pos.CENTER);
27         pane.setPadding(new Insets(10, 10, 10, 10));
28         pane.setHgap(10);
29         pane.setVgap(10);
30
31         // Create a button to choose a file
32         Button btChooseFile = new Button("Choose File");
33         pane.add(btChooseFile, 0, 0);
34
35         // Set the primary stage properties
36         primaryStage.setScene(new Scene(pane, 400, 200));
37         primaryStage.setTitle("Starting...");
38         primaryStage.setResizable(false);
39         primaryStage.show();
40
41         //
42         btChooseFile.setOnAction(new EventHandler<ActionEvent>() {
43             @Override
44             public void handle(ActionEvent event) {
45                 FileChooser fileChooser = new FileChooser();
46                 fileChooser.setTitle("Choose File");
47                 fileChooser.setInitialDirectory(new File("J:" + File.separator + "PrtSc" + File.separator + "20190321"));
48                 fileChooser.getExtensionFilters().addAll(new ExtensionFilter("Text Files", "*.txt"), new ExtensionFilter("All Files", "*.*"));
49                 File file = fileChooser.showOpenDialog(primaryStage);
50                 if (file != null) {
51                     System.out.println(file.getAbsolutePath());
52                 }
53             }
54         });
55     }
56 }

UI:

例2:增加一个缓存文件,用于记录上一次选中的目录。

工程结构:

第62行的代码,涉及到的知识点:Java - byte[] 和 String互相转换,

 1 import java.io.File;
 2 import java.io.FileInputStream;
 3 import java.io.FileNotFoundException;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 import java.io.InputStream;
 7 import java.io.OutputStream;
 8
 9 import javafx.application.Application;
10 import javafx.event.ActionEvent;
11 import javafx.event.EventHandler;
12 import javafx.geometry.Insets;
13 import javafx.geometry.Pos;
14 import javafx.scene.Scene;
15 import javafx.scene.control.Button;
16 import javafx.scene.layout.GridPane;
17 import javafx.stage.FileChooser;
18 import javafx.stage.FileChooser.ExtensionFilter;
19 import javafx.stage.Stage;
20
21 public class Main extends Application {
22
23     public static void main(String[] args) {
24         launch(args);
25     }
26
27     @Override
28     public void start(Stage primaryStage) throws Exception {
29         // Create a pane to hold a button
30         GridPane pane = new GridPane();
31         pane.setStyle("-fx-border-color: green;");
32         pane.setAlignment(Pos.CENTER);
33         pane.setPadding(new Insets(10, 10, 10, 10));
34         pane.setHgap(10);
35         pane.setVgap(10);
36
37         // Create a button to choose a file
38         Button btChooseFile = new Button("Choose File");
39         pane.add(btChooseFile, 0, 0);
40
41         // Set the primary stage properties
42         primaryStage.setScene(new Scene(pane, 400, 200));
43         primaryStage.setTitle("Starting...");
44         primaryStage.setResizable(false);
45         primaryStage.show();
46
47         //
48         btChooseFile.setOnAction(new EventHandler<ActionEvent>() {
49             @Override
50             public void handle(ActionEvent event) {
51                 FileChooser fileChooser = new FileChooser();
52                 fileChooser.setTitle("Choose File");
53                 fileChooser.getExtensionFilters().addAll(new ExtensionFilter("All Files", "*.*"));
54                 //
55                 File cacheFile = new File("cache.txt");
56                 if (cacheFile.exists()) {
57                     try (InputStream inputStream = new FileInputStream(cacheFile)) {
58                         byte[] bytes = new byte[(int) cacheFile.length()];
59                         // Read the contents of the cache.txt
60                         inputStream.read(bytes);
61                         //
62                         File directory = new File(new String(bytes));
63                         if (directory.exists()) {
64                             fileChooser.setInitialDirectory(directory);
65                         }
66                     } catch (FileNotFoundException e) {
67                         e.printStackTrace();
68                     } catch (IOException e1) {
69                         e1.printStackTrace();
70                     }
71                 }
72                 //
73                 File file = fileChooser.showOpenDialog(primaryStage);
74                 if (file != null) {
75                     // Store the directory to the cache.txt
76                     try (OutputStream outputStream = new FileOutputStream(cacheFile)) {
77                         byte[] bytes = file.getParent().getBytes();
78                         outputStream.write(bytes);
79                     } catch (FileNotFoundException e) {
80                         e.printStackTrace();
81                     } catch (IOException e) {
82                         e.printStackTrace();
83                     }
84                 }
85             }
86         });
87     }
88 }

转载于:https://www.cnblogs.com/Satu/p/10847649.html

JavaFX FileChooser文件选择器,缓存上一次打开的目录相关推荐

  1. 怎么批量取消Excel文件中的上下标

    今天跟大家分享一下怎么批量取消Excel文件中的上下标 1.打开Excel文件 2.选中要处理的单元格区域 3.点击下图选项(Excel工具箱,百度即可了解详细下载安装信息,本文这里就不做详细解说.) ...

  2. nginx 限制文件上传速度_nginx上传文件速度慢 Nginx上传文件全部缓存解决方案 - 硬件设备 - 服务器之家...

    nginx上传文件速度慢 Nginx上传文件全部缓存解决方案 发布时间:2017-03-09 来源:服务器之家 下面通过文字说明给大家详解Nginx上传文件全部缓存解决方案. 因为应用服务器(Jett ...

  3. javafx中css选择器_JavaFX技巧12:在CSS中定义图标

    javafx中css选择器 当您是像我这样来自Swing的UI开发人员时,您很有可能仍在代码中直接设置图像/图标. 最可能是这样的: import javafx.scene.control.Label ...

  4. java无刷新上传图片_【java实现web文件无刷新上传】

    最近在做如何实现文件上传的相关工作,查阅了很多资料,发现网上写的都不是很直观,且调试复杂,经实验成功. 把form的target设为页面里一个看不见的iframe,这样上传时候就不会刷新页面了,比如 ...

  5. 细说ASP.NET Core静态文件的缓存方式

    一.前言 我们在优化Web服务的时候,对于静态的资源文件,通常都是通过客户端缓存.服务器缓存.CDN缓存,这三种方式来缓解客户端对于Web服务器的连接请求压力的. 本文指在这三个方面,在ASP.NET ...

  6. vue+element-ui大文件的分片上传和断点续传js-spark-md5和browser-md5-file

    注意:以下共两份代码片段,第一份为原博主链接代码,第二份自己写的整体代码(比较乱) 1.参考 https://www.cnblogs.com/kelelipeng/p/10158599.html (j ...

  7. 清楚linux缓存文件,Linux删除文件 清除缓存

    相信很多测试 经常会经历开发叫你清除缓存这种事. 那我们要怎么清呢? 一.首先,确认你要清除的缓存在哪个目录下,然后切换到该目录下,比如 我现在知道我的的缓存目录是在newerp这个目录下,则如图 二 ...

  8. jquery 文件上传 触发两次_点击三次input按钮,前两次不选择任何文件,第三次选择一个文件,结果上传了3个文件,即发生了三次请求...

    点击三次input按钮,前两次不选择任何文件,第三次选择一个文件,结果页面上出现了3个文件,也就是上传了3个文件 我点击input,不选择任何文件,然后取消文件选择器的窗口,然后第二次次点击input ...

  9. linux下编辑文件实验,Linux上最常用的文本编辑器vi/vim使用教程

    vi(vim)是上Linux非常常用的编辑器,很多Linux发行版都默认安装了vi(vim).vi(vim)命令繁多但是如果使用灵活之后将会大大提高效率.vi是"visual interfa ...

最新文章

  1. Label Assign综述:提升目标检测上限
  2. python fieldnames_csvreader.fieldnames在python中未被识别为csv reader对象的属性
  3. cassandra可视化工具_一位数据科学家的私房工具清单
  4. Base64转BufferedImage
  5. easyui的textbox绑定focus事件(子页面
  6. Ansible 运维自动化 ( 配置管理工具 )
  7. mybatis_plus条件构造器
  8. 前期交互流程(PTES的第一步)
  9. linux文件系统 ext3,Linux环境中使用Ext3文件系统
  10. 28张高清数据分析全知识地图,强烈建议收藏
  11. 【iOS】XCode14 iOS16适配 pod签名 12.1闪退
  12. 7-19 统计人数(2008慈溪) (100分)
  13. Android UI自动化工具-SoloPi
  14. solr(4)solr7.3.1 使用DIH上传结构化数据
  15. access身份证号掩码_access中怎么设置掩码控制数字范围
  16. python判断一个数是否在范围内_检查数字列表是否在某个范围内?
  17. MySQL数据库——视图
  18. 股票的区分: 什么是 A股,B股,H股,N股?
  19. 嵌入式C语言学习第二天
  20. PCB行业ERP解决方案!重点推荐!!!

热门文章

  1. PHP中文乱码的常见解决方法总结
  2. synamic-datasource-spring-boot-starter实现动态数据源Mysql和Sqlserver
  3. ASP.NET中使用Cache类来缓存页面的信息
  4. Nginx在开发中常用的基础命令
  5. C#中快速设置控件的相关事件
  6. Vue怎样传递和获取路由参数
  7. java垃圾回收算法超详细全解
  8. class文件反编译-指令字节码对照表
  9. 使用 Flutter 制作一个简单的笑话生成器应用程序
  10. VS Code识别编辑规范,ESlint规则,VS Code保存去掉自动加分号、逗号、双引号