编辑:
工作代码:
Scanner input = new Scanner(System.in);
String inputFile = "Computer_Store_DSV.txt";
String userInputFile;
File sourceFile;
do {
System.out.println("Please enter the path of the file, enter to use the default path");
userInputFile = input.nextLine();
if (!"".equals(userInputFile)) {
inputFile = userInputFile;
}
sourceFile = new File(inputFile);
System.out.println("Path is = " + inputFile);
if (!sourceFile.canRead()) {
System.out.println("Unable to read the file");
}
} while (!sourceFile.canRead());
Scanner record = new Scanner(sourceFile);
感谢呼吸的帮助。
我有以下代码:
Scanner input = new Scanner(System.in);
boolean fileLoop = false;
String inputFile = "Computer_Store_DSV.txt";
String userInputFile;
do {
System.out.println("Please enter the path of the file, enter to use the default path");
userInputFile = input.nextLine();
if (!"".equals(userInputFile)) {
inputFile = userInputFile;
}
File sourceFile = new File(inputFile);
System.out.println("Path is = " + inputFile);
if (!sourceFile.canRead()) {
System.out.println("Unable to read the file");
fileLoop = true;
}
while (fileLoop);
Scanner record = new Scanner(sourceFile);
如您所见,我正在尝试在启动邮件程序之前“验证”文件是否存在。
有没有办法在不使用 File 类两次“验证和实际程序”的情况下做到这一点?