Java 如何处理FileNotFoundException?
我正在修补一个小应用程序,以便从文件中读取一些数字。到目前为止,一切运行良好,但现在我遇到了一个问题,我不知道如何才能有效地解决它。如果用户无意中输入了错误的文件名,JVM将抛出FileNotFoundException,我在调用方法中捕捉到了它。现在我想给他(用户)两次输入正确文件名的尝试,但我不知道如何再次调用该方法,当我实际在下面的catch块中时,该方法正在打开文件。 我将在下面说明我的瞬态解决方案,但我不确定这是否是解决此问题的最有效/最优雅的方法:Java 如何处理FileNotFoundException?,java,exception-handling,filenotfoundexception,Java,Exception Handling,Filenotfoundexception,我正在修补一个小应用程序,以便从文件中读取一些数字。到目前为止,一切运行良好,但现在我遇到了一个问题,我不知道如何才能有效地解决它。如果用户无意中输入了错误的文件名,JVM将抛出FileNotFoundException,我在调用方法中捕捉到了它。现在我想给他(用户)两次输入正确文件名的尝试,但我不知道如何再次调用该方法,当我实际在下面的catch块中时,该方法正在打开文件。 我将在下面说明我的瞬态解决方案,但我不确定这是否是解决此问题的最有效/最优雅的方法: //code omitted
//code omitted
int temp = 0;
while(true) {
filename = input.next();
try {
ex.fileOpen(filename);
}
catch(FileNotFoundException e) {
if(temp++ == 3) {
System.err.println("You have entered the filename three times consecutively wrongly");
return;
}
continue;
}
break;
}
//do some other stuff
输入是一个扫描仪,它读取用户输入并将其分配给字符串变量filename。fileOpen是一种获取文件名、打开文件、读取内容并将所有数字写入向量的方法
因此,我非常感谢更有经验的java程序员提供的一切支持
问候
Tom您可以使用
文件
类的exists
方法
例如,fileOpen
方法可以返回true/false,无论文件是否存在我认为这会起作用
int x = 0;
while (true){
filename = input.next();
try{
ex.fileOpen(filename);
break; // If it throws an exeption, will miss the break
}catch(FileNotFoundException e){
System.err.println("File not found, try again.");
}
if (x==2){
System.errprintln("You have entered the wrong file 3 times");
System.exit(0);
}
x++
}
你可以用这样的东西
public class AppMain {
public static void main(String[] args) throws IOException {
String filePath = input.next();
InputStream is = getInputStream(filePath);
int temp = 0;
while(is == null && temp < 3){
filePath = input.next();
is = getInputStream(filePath);
temp++;
}
if(is == null){
System.err.println("You have entered the filename three times consecutively wrongly");
return;
}
.........
.........
}
private static InputStream getInputStream(String filePath){
InputStream is = null;
try{
is = new FileInputStream(filePath);
return is;
}catch (IOException ioException) {
return null;
}
}
}
公共类AppMain{
公共静态void main(字符串[]args)引发IOException{
字符串filePath=input.next();
InputStream is=getInputStream(文件路径);
内部温度=0;
while(is==null&&temp<3){
filePath=input.next();
is=getInputStream(文件路径);
temp++;
}
如果(is==null){
System.err.println(“您连续错误地输入了三次文件名”);
返回;
}
.........
.........
}
私有静态InputStream getInputStream(字符串文件路径){
InputStream=null;
试一试{
is=新文件输入流(文件路径);
回报是;
}捕获(IOException IOException){
返回null;
}
}
}
您可能希望再次递归调用该方法:
public void doTheStuff(int attemptsLeft)
// ...
if (attemptsLeft == 0) {
System.err.println("You have entered the filename three times consecutively wrongly");
return;
}
filename = input.next();
try {
ex.fileOpen(filename);
}
catch(FileNotFoundException e) {
doTheStuff(attemptsLeft - 1);
return;
}
// ...
}
然后只需调用
doTheStuff(3)
这样的东西(伪代码,不可执行)怎么样
/。。。
对于(int i=0;i<3;i++)
{
//获取文件名的用户交互
如果(尝试打开文件(ex))
{
打破
}
}
//检查文件是否在此打开并进行适当处理。
// ...
}
bool attemptoopenfile(File ex,String filename){//忘记了这个类的类名
试一试{
例如fileOpen(文件名);
返回true;
}catch(filenotfounde异常){
返回false;
}
}
或者,在调用fileOpen()之前检查文件是否存在。不要使用异常来控制您的工作流。试着这样做:
final int MAX_ERROR_ALLOWED=3;
public void readFile(String filename, int errorCount){
try{
File f = new File(filename);
if(!f.exists()){
String newFilename = input.next();
if(errorCount>=MAX_ERROR_ALLOWED){
throw new JustMyException();
}
readFile(newFilename, errorCount++);
}else{
//whatever you need to do with your file
}
}
}
检查此链接,它显示如何检查文件是否存在异常是由JVM引发的,而不是由编译器引发的。请精确@TiagoAlmeida他已经有了一个简单的方法来检查文件是否存在。如果抛出FileNotFoundException,则该链接中的内容是100%冗余的。@EJP感谢您的提示。下次我会尽量说得更准确;)很好的解决方案。这正是我想要的。要让一个私人方法检查文件是否已成功打开:)我只是没有想到…好主意,但我不确定这是否真的会起作用?我的意思是,在到达return语句之后,程序将跳回catch处理程序,我认为它将在catch语句之后继续执行程序。。。然后,程序将处理未打开的文件。
final int MAX_ERROR_ALLOWED=3;
public void readFile(String filename, int errorCount){
try{
File f = new File(filename);
if(!f.exists()){
String newFilename = input.next();
if(errorCount>=MAX_ERROR_ALLOWED){
throw new JustMyException();
}
readFile(newFilename, errorCount++);
}else{
//whatever you need to do with your file
}
}
}