Scanner read=new Scanner(file);
此语句中出现异常“FileNotFoundException”。
import java.util.*;
import java.io.*;
public class shoppingList {
public static void main(String []args)throws IOException {
File file=new File("MyList.txt");
try {
if(file.exists()==false)
throw new FileNotFoundException("the file input doesn't exist");
}
catch(FileNotFoundException e){System.out.print(e.getMessage());}
//I tried handling the exception but it didn't work
Scanner read=new Scanner(file);
File outfile=new File("MyReceipt.txt");
FileOutputStream fos=new FileOutputStream(outfile);
PrintWriter output=new PrintWriter(fos);
while(read.hasNext()) {
String item=read.next();
double price=read.nextDouble();
String status=read.next();
output.println("My Receipt: ");
output.println("--------------------");
if(status.equals("Done")==true)
output.println(item+" "+price);
double total=0;
total+=price;
output.println("--------------------");
output.println("total= "+total);
}
read.close();
output.close();
}
}
答案 0 :(得分:2)
问题是程序在catch语句之后才会继续。因此,即使您处理file.exists
抛出的第一个FileNotFoundException,您也会得到Scanner read=new Scanner(file);
的第二个,而且这个不会被处理。
答案 1 :(得分:1)
抓住FileNotFoundException
后,您尝试读取另一个文件并抛出另一个FileNotFoundException
问题在于:
Scanner read=new Scanner(file);
解决方案是将所有代码放在try块中:
File file=new File("MyList.txt");
try{
if(file.exists()==false)
throw new FileNotFoundException("the file input doesn't exist");
//i tried handling the exception but it didn't work
Scanner read;
read = new Scanner(file);
File outfile=new File("MyReceipt.txt");
FileOutputStream fos;
fos = new FileOutputStream(outfile);
PrintWriter output=new PrintWriter(fos);
while(read.hasNext()){
String item=read.next();
double price=read.nextDouble();
String status=read.next();
output.println("My Receipt: ");
output.println("--------------------");
if(status.equals("Done")==true)
output.println(item+" "+price);
double total=0;
total+=price;
output.println("--------------------");
output.println("total= "+total);
}
read.close();
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
答案 2 :(得分:1)
new File("MyList.txt")
将尝试在当前目录中查找该文件。
当前目录取决于运行程序的环境。例如,如果程序在Eclipse IDE中运行,则Java项目是当前目录。
尝试提供绝对路径。例如。 C:\\workspace\\project\\MyList.txt
。
或者,将文件放在源树或包下,然后按类路径打开InputStream
。例如,如果将其放置在名为my.foo.shopping
的包中,您可以直接创建Scanner
,如下所示。
Scanner read=new Scanner(shoppingList.class.getResourceAsStream("/my/foo/shoppin/MyList.txt"));
答案 3 :(得分:0)
try {
if(file.exists()==false)
throw new FileNotFoundException("the file input doesn't exist");
}
catch(FileNotFoundException e){System.out.print(e.getMessage());}
//I tried handling the exception but it didn't work
Scanner read=new Scanner(file);
此处的最后一行显示,在try-catch外部是您创建Scanner read=new Scanner(file);
的问题所在,如果该文件不存在,则无法阻止扫描程序在尝试时崩溃访问此文件。
您应该将行Scanner read=new Scanner(file);
放在try-catch中,如下所示:
try {
if(file.exists()){
Scanner read=new Scanner(file);
}
else if(file.exists()==false)
throw new FileNotFoundException("the file input doesn't exist");
}
}
如果文件不存在,这应该可以防止您的程序崩溃。
答案 4 :(得分:0)
尝试使用文件类的getAbsolutePath()来查看您正在使用的目录,并且您的文件是否存在于同一目录中。否则考虑给出完整的路径。