簡體   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