简体   繁体   中英

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

2) Then you have to mention path like "C:\\\\Users\\...". Make sure you use right way to mention file names on windows system

3) You have to again check similarly for FileOutputStream.

You need to separate your directories with

File.separator

Which will hold the value of / , : or \\\\ depending on which platform you are on.

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