Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java filenotfoundexception在找到文件之前询问_Java_File_Exception - Fatal编程技术网

Java filenotfoundexception在找到文件之前询问

Java filenotfoundexception在找到文件之前询问,java,file,exception,Java,File,Exception,我必须写一个支持扩展的程序。我遇到FileNotFound异常问题。程序询问文件名。我的任务是在没有像给定的文件时写入一个特殊信息,并再次询问,直到用户写入一个现有的文件名。我知道如何编写没有文件的特殊信息,但我不知道如何再次询问文件名(我只知道必须使用readNP方法)。 代码如下: import.java.io.*; import java.util.* ; class Reading{ static BufferedReader sysin = new Buf

我必须写一个支持扩展的程序。我遇到FileNotFound异常问题。程序询问文件名。我的任务是在没有像给定的文件时写入一个特殊信息,并再次询问,直到用户写入一个现有的文件名。我知道如何编写没有文件的特殊信息,但我不知道如何再次询问文件名(我只知道必须使用readNP方法)。 代码如下:

import.java.io.*;
import java.util.* ;

class Reading{
   static BufferedReader sysin =
           new BufferedReader(new InputStreamReader(System.in));

   String readNP() throws IOException{
   //  ask about file name
      System.out.print("file name ");
      String filename;
      filename = sysin.readLine() ;
      return filename.trim();
}

void read(ArrayList a)引发IOException{
//从文件中读取
int nr=1;
字符串名称=readNP();
BufferedReader br=新的BufferedReader(新文件读取器(名称));
弦线;
而((line=br.readLine())!=null){
a、 增加(新的双(行));
nr++;
}
br.close();
}
}
类异常{
静态空隙平均值(ArrayList a){
双s=0.0d;

对于(int i=0;i,我将在
readNP()函数中添加文件名是否存在的测试(并且不是目录名):

String readNP() throws IOException{
//  ask about file name
    for (;;) {
        System.out.print("file name ");
        String filename;
        filename = sysin.readLine();
        File f = new File(filename);
        if (f.exists() && !f.isDirectory()) return filename.trim();
        System.out.println("file absent");  //message if the there is no file with this name
    }
}

你的问题是什么?谢谢,就是这样!:)@user3491139:好的,太好了!但实际上这个方法不支持FileNotFoundException。我试着给出类似于{抛出新的FileNotFoundException();}if之后,但它只执行一次,而不是在一个循环中执行。@user3491139:但我认为您并不是真的想抛出异常,因为这样会使重复请求正确的文件名变得不可能。
String readNP() throws IOException{
//  ask about file name
    for (;;) {
        System.out.print("file name ");
        String filename;
        filename = sysin.readLine();
        File f = new File(filename);
        if (f.exists() && !f.isDirectory()) return filename.trim();
        System.out.println("file absent");  //message if the there is no file with this name
    }
}