处理FileNotFoundException

时间:2013-11-22 16:04:07

标签: java methods return

我想知道这是不是一个好主意,或者是否在初始调用时使用while循环更好?或其他什么?

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String[] string = thisSameMethod(input);
}

public static String[] thisSameMethod(Scanner input){

  //Asking user for filename here

  try {
    //open and read file
  } catch (FileNotFoundException e) {
    System.out.println("File Not found!\n Try again:");
    return thisSameMethod(input); //returning itself
  }
}

编辑:这会是怎样的呢? 像这样生成main,并在catch中放入“return null”:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String[] string = null;
        while(string==null)
         string = thisSameMethod(input); 
}

5 个答案:

答案 0 :(得分:3)

这是一个可怕的想法,因为它是无限递归的一个例子。在基本级别,以下是更好的解决方案:

public static String[] blammy(final Scanner input)
{
    boolean success = false;

    while (!success)
    {
        try
        {
            // stuff.
            success = true;
        }
        catch (FileNotFouncException exception)
        {
            // error stuff here.
        }
    }
}

答案 1 :(得分:0)

没有。这将进入一个无限循环,thisSameMethod()以递归方式自我调用,直到输入有效文件并且没有错误打开它。

在这种情况下,它也可能导致内存泄漏,因为尝试的文件对象永远不会关闭。您需要在发生错误后关闭资源,通常在finally块中。

 try (File f = new File())
 {
 }
 catch(Exception e)
 {
 }
 finally
 {
   f.close();
 }

如果您使用Java 7,则另一个选项是使用try-with-resources块。

 try (File f = new File())
 {
 }
 catch(Exception e)
 {
 }

当您离开try的范围时,这将始终关闭资源。在您的情况下,这意味着所有文件操作也需要在您的方法中完成。

答案 2 :(得分:0)

不。这将无限执行并调用操作;你需要不返回任何内容(所以将此函数更改为void)或者如上所述,使用循环和布尔变量来阻止它反复发生。

答案 3 :(得分:0)

我想这段代码会做你想做的事,

public static class InvalidInputException extends Exception {

    public InvalidInputException(String message, Throwable cause) {
        super(message, cause);
    }
}

public static String[] getContentFromFile(String filePath)
        throws InvalidInputException {
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(filePath);
        // Read it to array

        return null;
    } catch (FileNotFoundException e) {
        throw new InvalidInputException("Erro!", e);
    } finally {
        if (fis != null) {
            try {
                fis.close();
            } catch (IOException e) {
                // Close silently
            }
        }
    }

}

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);

    String[] fileContent = null;
    do {
        System.out.println("Please, input the file location!");
        String nextLine = scanner.nextLine();
        try {
            fileContent = getContentFromFile(nextLine);
        } catch (InvalidInputException e) {

            Throwable cause = e.getCause();
            StringBuilder sb = new StringBuilder();
            sb.append("Something went wrong ");
            sb.append(e.getMessage());

            if (cause != null) {
                sb.append(" ");
                sb.append(cause.getMessage());
            }
            System.out.println(sb.toString());
        }
    } while (fileContent == null);

}

答案 4 :(得分:0)

你试过这个:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String[] string = thisSameMethod(input);
}

public static String[] thisSameMethod(Scanner input){
   Scanner newInput = new Scanner(System.in);
  //Asking user for filename here

  try {
    //open and read file
  } catch (FileNotFoundException e) {
    System.out.println("File Not found!\n Try again:");
    System.out.println("Please Enter Another file to look for: );
    String newFile = newInput.next();
    thisSameMethod(newInput);
  }
}