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();
}
存在しないファイル名を入力してもループが繰り返されると、存在するファイル名によってループがトリガーされます。どうして?宿題:ファイルが存在しないことを確認するためにループを使用する