当我为“搜索”输入完整的文件路径(C:\ Users \ djustinwebb \ Documents \ BlueJ Projects \ LightHouse)时,它试图打开文件,我得到错误: java.io.FileNotFoundException:C:\ Users \ djustinwebb \ Documents \ BlueJ Projects \ LightHouse(访问被拒绝)(在java.io.FileInputStream中)如何避免此java.io.FileNotFoundException?
我输入“invoicedata.txt”进行搜索,即使它没有读取文件正确,但我想知道为什么当我使用完整的文件路径时不起作用。我需要做什么来使用完整的文件路径而不会遇到这个错误?
public String searchCase()throws FileNotFoundException
{
String fileLine = null;
StringTokenizer stok = null;
Scanner inputFile = new Scanner(new File(search));
String whatever = null;
while(inputFile.hasNextLine())
{
fileLine = inputFile.nextLine();
stok = new StringTokenizer(fileLine,",");
caseLCount++;
while(stok.hasMoreTokens())
{
if(userWord.equals(stok.nextToken()))
{
caseWCount++;
whatever += caseLCount + ".\n";
}//end if
}// end nested while
}//end outer while
inputFile.close();
return whatever;
}// end searchCase()
该文件是否真的存在于该路径?你可以从另一个程序打开文件吗?尝试以管理员模式运行Java进程。 –
似乎是权限问题。它拒绝访问的事实意味着它确实尝试打开文件 – Mox