Java 家庭作业:使用循环验证文件是否存在';不存在

Java 家庭作业:使用循环验证文件是否存在';不存在,java,Java,一旦循环由存在的文件名触发,即使输入了不存在的文件名,循环也会不断重复。为什么?您没有在每次迭代中实际测试新文件的存在性,只是在标题1上测试.exists(),因为它在循环的每次迭代之前都被实例化。您没有在每次迭代中实际测试新文件的存在性,只是测试.exists()在循环之前实例化的标题1上,用于循环的每次迭代。因为在while循环中,没有修改标题1的地方。因为在while循环中,没有修改标题1的地方。循环不会退出,因为标题1仍然指向旧文件(已存在的变量)。您需要更新变量:

一旦循环由存在的文件名触发,即使输入了不存在的文件名,循环也会不断重复。为什么?

您没有在每次迭代中实际测试新文件的存在性,只是在标题1上测试.exists(),因为它在循环的每次迭代之前都被实例化。

您没有在每次迭代中实际测试新文件的存在性,只是测试.exists()在循环之前实例化的标题1上,用于循环的每次迭代。

因为在while循环中,没有修改
标题1的地方。

因为在while循环中,没有修改
标题1的地方。

循环不会退出,因为
标题1仍然指向旧文件(已存在的变量)。您需要更新变量:

              String filename1; //file name to write to

    //get the file name to write to 
    System.out.println("\nEnter the filename for the file where the information will be stored:");
    filename1=keyboard.nextLine(); 
              File title1= new File(filename1);

    while (title1.exists()) //make sure the file doesn't exist
    {
        System.out.println("The file " + filename1 +" already exists.");
        System.out.println("Please choose another name.");
        System.out.println("Enter the filename:");
        filename1=keyboard.nextLine();
    }

循环不会退出,因为
title1
仍然指向旧文件(已存在的文件)。您需要更新变量:

              String filename1; //file name to write to

    //get the file name to write to 
    System.out.println("\nEnter the filename for the file where the information will be stored:");
    filename1=keyboard.nextLine(); 
              File title1= new File(filename1);

    while (title1.exists()) //make sure the file doesn't exist
    {
        System.out.println("The file " + filename1 +" already exists.");
        System.out.println("Please choose another name.");
        System.out.println("Enter the filename:");
        filename1=keyboard.nextLine();
    }
为什么?

因为您没有更新标题1
。事实上,您一直在测试用户提供的第一个(不存在的)文件名


代码中还有另一个(相当模糊的)错误。如果用户输入EOF字符(例如^D),对
readLine()
的调用将返回
null
,然后您的代码将尝试对null引用调用
exists()
,从而导致
NullPointerException

为什么?

因为您没有更新标题1
。事实上,您一直在测试用户提供的第一个(不存在的)文件名



代码中还有另一个(相当模糊的)错误。如果用户输入EOF字符(例如^D),调用
readLine()
将返回
null
,然后您的代码将尝试调用
exists()
在空引用上…导致
空点异常

您总是检查第一个文件是否存在,您应该使用新文件名初始化它

String filename1; //file name to write to

//get the file name to write to 
System.out.println("\nEnter the filename for the file where the information will be stored:");
filename1=keyboard.nextLine(); 
File title1= new File(filename1);

while (title1.exists()) //make sure the file doesn't exist
{
    System.out.println("The file " + filename1 +" already exists.");
    System.out.println("Please choose another name.");
    System.out.println("Enter the filename:");
    filename1=keyboard.nextLine();

    title1 = new File(filename1);
}

您总是检查第一个文件是否存在,您应该使用新文件名初始化它

String filename1; //file name to write to

//get the file name to write to 
System.out.println("\nEnter the filename for the file where the information will be stored:");
filename1=keyboard.nextLine(); 
File title1= new File(filename1);

while (title1.exists()) //make sure the file doesn't exist
{
    System.out.println("The file " + filename1 +" already exists.");
    System.out.println("Please choose another name.");
    System.out.println("Enter the filename:");
    filename1=keyboard.nextLine();

    title1 = new File(filename1);
}
当您这样做时:

 File title1= new File(filename1);
 while (title1.exists()) //make sure the file doesn't exist
{
    System.out.println("The file " + filename1 +" already exists.");
    System.out.println("Please choose another name.");
    System.out.println("Enter the filename:");
    filename1=keyboard.nextLine();
    title1= new File(filename1);   
}
您所做的只是更新保存在
filename1
中的字符串值。即使在创建
title1
时使用了
filename1
,这种关系也不会像您想象的那样得到维护

您需要做的是使用新文件名创建一个新文件,然后再次执行相同的测试:

filename1=keyboard.nextline();
此外,通常最好将控制变量(表示是否继续执行循环的位)存储在自己的变量中,而不是调用方法:

filename1=keyboard.nextLine();
// re-create file 1 with the new filename stored in filename1
这将使您的代码看起来像这样:

Boolean fileExists = title1.exists();

while(fileExists) {
    ....
}
当您这样做时:

 File title1= new File(filename1);
 while (title1.exists()) //make sure the file doesn't exist
{
    System.out.println("The file " + filename1 +" already exists.");
    System.out.println("Please choose another name.");
    System.out.println("Enter the filename:");
    filename1=keyboard.nextLine();
    title1= new File(filename1);   
}
您所做的只是更新保存在
filename1
中的字符串值。即使在创建
title1
时使用了
filename1
,这种关系也不会像您想象的那样得到维护

您需要做的是使用新文件名创建一个新文件,然后再次执行相同的测试:

filename1=keyboard.nextline();
此外,通常最好将控制变量(表示是否继续执行循环的位)存储在自己的变量中,而不是调用方法:

filename1=keyboard.nextLine();
// re-create file 1 with the new filename stored in filename1
这将使您的代码看起来像这样:

Boolean fileExists = title1.exists();

while(fileExists) {
    ....
}