如何选择Java文件输出路径

问题描述 投票:0回答:1

我有这样的加密代码正在开发,但问题是,一旦我选择的文件加密和加密,其保存在默认净豆项目文件夹,并具有类似名称的所有加密的文件。我希望能够选择文件的位置,并重新命名加密输出,任何帮助将不胜感激。

这里是的代码的一部分的样本

JFileChooser chooser = new JFileChooser();
    chooser.showOpenDialog(null);
    File f = chooser.getSelectedFile();
    file_path.setText(f.getAbsolutePath());
}                                        

private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
    try{
        FileInputStream file = new FileInputStream(file_path.getText());
        FileOutputStream outStream = new FileOutputStream("Encrypt.mp4");
        byte k[]="Crot2116MpFr5252".getBytes();
        SecretKeySpec key = new SecretKeySpec(k, "AES");
        Cipher enc = Cipher.getInstance("AES");
        enc.init(Cipher.ENCRYPT_MODE, key);
        CipherOutputStream cos = new CipherOutputStream(outStream, enc);
        byte[] buf = new byte[1024];
        int read;
        while((read=file.read(buf))!=-1){
            cos.write(buf,0,read);
        }
        file.close();
        outStream.flush();
        cos.close();
        JOptionPane.showMessageDialog(null, "The Video file was encrypted Successfully");
    }catch(Exception e){
        JOptionPane.showMessageDialog(null, "Invalid selection");
    }
}                                        
java
1个回答
0
投票

您应指定在FileOutputStream的完整路径:

    FileOutputStream outStream = new FileOutputStream("/path/to/file");

这里的绝对路径正被使用(在文件系统管理器的根路径)。您还可以使用该项目的相对路径,而无需把第一/

    FileOutputStream outStream = new FileOutputStream("path/to/file");

像这样的文件将被保存在src/main/path/to/file

© www.soinside.com 2019 - 2024. All rights reserved.