繁体   English   中英

文件存在,但是当我在该文件上执行操作时,系统显示FileNotFoundException异常。 当文件位于源代码所在的目录中时有效

[英]File exists but when I perform operation on that file, system shows FileNotFoundException. Works when file is in dir where source code is

public void decrypt() throws Exception
{
    //opening streams
    //Error is in the line below When i try to read file from directory
    //other than the one which has .java and .class files.
    FileInputStream fis1 =new FileInputStream(file);
    File dir=new File("C:/Crypt-R/Decrypted");
    dir.mkdirs();
    file=new File(dir,file.getName() +".dec");
    FileOutputStream fos1 =new FileOutputStream(file);  
    //generating same key
    byte k[] = keyRecv.getBytes();   
    SecretKeySpec key = new SecretKeySpec(k,"AES");  
    //creating and initialising cipher and cipher streams
    Cipher decrypt =  Cipher.getInstance(algorithm);  
    decrypt.init(Cipher.DECRYPT_MODE, key);  
    CipherInputStream cin=new CipherInputStream(fis1, decrypt);
    byte[] buf = new byte[1024];
    int read=0;
    while((read=cin.read(buf))!=-1)  //reading encrypted data from file
    {
    fos1.write(buf,0,read);       //writing decrypted data to file
    }
    //closing streams
    cin.close();
    fos1.flush();
    fos1.close();
    JOptionPane.showMessageDialog (null,
    "File Decrypted",
    "Success..!!",
    JOptionPane.INFORMATION_MESSAGE);
}

有连接到该程序的文本编辑器,文件被显示在编辑器中,但是当我试图解密它,它不会在我的源代码保持该目录存在,那么就说明文件中未发现异常。 你能帮我这个忙吗?

您需要检查以下内容

1)确保您提到的位置确实存在文件名检查获取FileNotFoundException的原因

2)然后,您必须提及路径“ C:\\\\ Users \\ ...”。 确保您使用正确的方式在Windows系统上提及文件名

3)您必须再次类似地检查FileOutputStream。

您需要用

File.separator

其将持有的价值/:\\\\取决于哪个平台上。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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