编程语言
首页 > 编程语言> > JavaFX FileChooser文件选择器,缓存上一次打开的目录

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

作者:互联网

例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:增加一个缓存文件,用于记录上一次选中的目录。

工程结构:

 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 cache = new File("cache.txt");
56                 if (cache.exists()) {
57                     try (InputStream inputStream = new FileInputStream(cache)) {
58 //                        int length = (int) cache.length();
59 //                        byte[] bytes = new byte[length];
60                         byte[] bytes = new byte[(int) cache.length()];
61                         // Read the contents of the cache.txt
62                         inputStream.read(bytes);
63                         //
64                         File directory = new File(new String(bytes));
65                         if (directory.exists()) {
66                             fileChooser.setInitialDirectory(directory);
67                         }
68                     } catch (FileNotFoundException e) {
69                         e.printStackTrace();
70                     } catch (IOException e1) {
71                         e1.printStackTrace();
72                     }
73                 }
74                 //
75                 File file = fileChooser.showOpenDialog(primaryStage);
76                 if (file != null) {
77                     // Store the directory to the cache.txt
78                     try (OutputStream outputStream = new FileOutputStream(cache)) {
79                         byte[] bytes = file.getParent().getBytes();
80                         int length = file.getParent().getBytes().length;
81                         outputStream.write(bytes, 0, length);
82 //                        outputStream.write(file.getParent().getBytes(), 0, file.getParent().getBytes().length);
83                     } catch (FileNotFoundException e) {
84                         e.printStackTrace();
85                     } catch (IOException e) {
86                         e.printStackTrace();
87                     }
88                 }
89             }
90         });
91     }
92 }

 

标签:10,pane,JavaFX,javafx,FileChooser,new,import,File,选择器
来源: https://www.cnblogs.com/Satu/p/10847649.html