我有这段代码:
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);
当我输入文件“C:/Users/Joe/Desktop/file.txt”的目的地时,结果是这个错误:
java.io.FileNotFoundException:“C:/Users/Joe/Desktop/file.txt”(The 文件名,目录名或卷标语法不正确)
但该文件存在,我该怎么办?
谢谢你..
答案 0 :(得分:2)
当我输入文件的目的地时 " C:/Users/Joe/Desktop/file.txt"
应提供不带引号的文件名("")
答案 1 :(得分:0)
如果您100%确定此文件存在于特定位置,那么这是两件事之一。另外,请尝试转义文件名中的/
如果处理不正确,Java将抛出此异常。在try ... catch()
区块中将您的陈述包围,或在导入throws FileNotFoundException
后放置java.io.FileNotFoundException
,如下所示:
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");
}
或
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);
}
另外,另一个原因是文件受到保护。如果您希望能够同时执行这两个操作,请将文件设置为只读或读取和写入。
答案 2 :(得分:0)
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 !!!");
}