Reputation: 231
i have this code :
BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Public Key to encrypt with: ");
String publicKeyFilename = in.readLine();
FileInputStream fis = new FileInputStream(publicKeyFilename);
when i enter the destination of the file "C:/Users/Joe/Desktop/file.txt", the result is this error:
java.io.FileNotFoundException: "C:/Users/Joe/Desktop/file.txt" (The filename, directory name, or volume label syntax is incorrect)
but the file exist, so what can i do?
Thank u..
Upvotes: 5
Views: 15837
Reputation: 436
when i enter the destination of the file "C:/Users/Joe/Desktop/file.txt"
Filename should be provided without quotes ("")
Upvotes: 2
Reputation: 1
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Public Key to encrypt with: ");
String publicKeyFilename = in.readLine();
try
{
FileInputStream fis = new FileInputStream(publicKeyFilename);
}
catch(Exception e)
{
System.out.println("File error !!!");
}
Upvotes: 0
Reputation: 7416
EDIT: I noticed that you were using forward slashes in your filename. If you're on windows, you want to use a backslash ()
If you're 100% sure that this file exists in the specific location, then it is one of two things. Also, try escaping the /
s in your filename
Java will throw this exception when it is not handled correctly. Surround your statement in a try ... catch()
block, or put throws FileNotFoundException
, after having imported java.io.FileNotFoundException
, like this:
import java.io.FileNotFoundException;
try{
BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Public Key to encrypt with: ");
String publicKeyFilename = in.readLine();
FileInputStream fis = new FileInputStream(publicKeyFilename);
}catch(FileNotFoundException e){
System.out.println("File does not exist");
}
or
import java.io.FileNotFoundException;
void encrypt throws FileNotFoundException(){
BufferedReader in = new BufferedReader
(new InputStreamReader(System.in));
System.out.print("Public Key to encrypt with: ");
String publicKeyFilename = in.readLine();
FileInputStream fis = new FileInputStream(publicKeyFilename);
}
Also, another reason is that the file is protected. Set the file to readonly, or read and write if you want to be able to do both.
Upvotes: 0