简体   繁体   中英

JavaFX Button for FileInputStream?

 runEncrypt.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {

                File inputFile = new File("/Users/aktasberk/Desktop/hey");
                File encryptedFile = new File("/Users/aktasberk/Desktop/Encrypted_"+inputFile.getName());
                File decryptedFile = new File("/Users/aktasberk/Desktop/Decrypted_"+inputFile.getName());

                try {
                    String key = "16BitKeyIsHere16";
                CryptoUtils.encrypt(key, inputFile, encryptedFile);
                  CryptoUtils.decrypt(key, encryptedFile, decryptedFile);
                } catch (CryptoException ex) {
                  System.out.println(ex.getMessage());
                ex.printStackTrace();
                }
        }
    });

Okay so I have an encryption&decryption project, the encrypting and decrypting works fine but I have some problems using FileInputStream to get the file from directory, I have a browse button to do that but could not make it work, so as you can see in the code I get the input file manually.

Below here is my browse button opening up a file dialog to let me choose a file.

browseEncrypt.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {


            File selectedFile = chooseEncrypt.showOpenDialog(primaryStage);
            if (selectedFile != null) {
                encryptPath.setText(selectedFile.getPath());
                primaryStage.show();
            }
        }
    });

I need to get the file from browse button instead of declaring it manually in the code, I can be more specific if info is needed, thanks.

Delete local:

File encryptedFile = new File("/Users/aktasberk/Desktop/Encrypted_"+inputFile.getName());

Make global

File encryptedFile;

Then:

browseEncrypt.setOnAction(new EventHandler<ActionEvent>() {
    @Override
    public void handle(ActionEvent event) {


        File selectedFile = chooseEncrypt.showOpenDialog(primaryStage);
        if (selectedFile != null) {
            encryptPath.setText(selectedFile.getPath());
            encryptedFile = selectedFile;//Add This!
            primaryStage.show();//Not sure why this is here?
        }
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM