简体   繁体   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);
}

There is a Text Editor attached to this program, file gets displayed in that Editor but when i am trying to decrypt it and it does not exist in the directory where my source code is kept then it shows file not found exception. 有连接到该程序的文本编辑器,文件被显示在编辑器中,但是当我试图解密它,它不会在我的源代码保持该目录存在,那么就说明文件中未发现异常。 Can you please help me out with this? 你能帮我这个忙吗?

You need to check the following things 您需要检查以下内容

1) Make sure you are mentioning filename that really exists in the location Check Reasons for getting FileNotFoundException 1)确保您提到的位置确实存在文件名检查获取FileNotFoundException的原因

2) Then you have to mention path like "C:\\\\Users\\...". 2)然后,您必须提及路径“ C:\\\\ Users \\ ...”。 Make sure you use right way to mention file names on windows system 确保您使用正确的方式在Windows系统上提及文件名

3) You have to again check similarly for FileOutputStream. 3)您必须再次类似地检查FileOutputStream。

You need to separate your directories with 您需要用

File.separator

Which will hold the value of / , : or \\\\ depending on which platform you are on. 其将持有的价值/:\\\\取决于哪个平台上。

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

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