英文:
How can I let user input the name of text file again if the name of file already exists?
问题
public static void textFileOpen(String fileName) throws IOException {
while (true) {
try {
FileReader fileReader = new FileReader(fileName);
LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
BufferedReader bufferedReader = new BufferedReader(fileReader);
fileReader.close();
bufferedReader.close();
lineNumberReader.close();
break; // Added to exit the loop if file exists
} catch (Exception ex) {
System.out.println("File " + fileName + " does not exist! Please try again.");
// Add code to read the user's input for the file name and update the 'fileName' variable
}
}
}
To fix the issue of the loop running forever, I added a break;
statement after successfully opening the file to exit the loop. Additionally, I added a comment indicating where you should add code to read the user's input for the file name and update the fileName
variable. This will allow the user to input a new file name and try again.
英文:
public static void textFileOpen(String fileName) throws IOException
{
while(true)
{
try
{
FileReader fileReader = new FileReader(fileName);
LineNumberReader lineNumberReader = new LineNumberReader(fileReader);
BufferedReader bufferedReader = new BufferedReader(fileReader);
fileReader.close();
bufferedReader.close();
lineNumberReader.close();
}
catch(Exception ex)
{
System.out.println("File " + fileName + " does not exists! Please try again.");
}
}
}
I'm trying to let the user input file name again if exists. But it runs forever if user enter an exist file name. How can I fix it? Can anyone help, please? THank you
答案1
得分: 1
原因是你使用了 try(input)
。因为在第一个 catch
后扫描器已经关闭,所以无法接受任何新输入。
移除 (input)
,并且使用单独的 input.close
应该可以解决问题。
英文:
The cause is your use of try(input)
. As the scanner has already been closed after the first catch
, it is not able to take any new input.
Remove the (input)
and use a separate input.close
should work.
答案2
得分: 1
你不应该关闭包装了System.in
的Scanner
,因为System.in
表示标准输入,通常是计算机键盘。因此,当你关闭它时,你的程序将无法从键盘接收输入。
你也不应该使用异常处理来测试条件。你应该使用条件语句,比如if
语句。
为了测试文件是否存在,你可以调用方法 isFile。该方法返回...
> 仅当此抽象路径名表示的文件存在且是普通文件时才返回true;否则返回false。
当你创建一个FileWriter
时,如果文件不存在,它也会创建该文件。
以下是演示上述内容的代码。
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Cipher {
public static void main(String[] args) throws IOException {
System.out.println("输入文件名:");
Scanner input = new Scanner(System.in);
String fileName = null;
File file = null;
while (true) {
fileName = input.nextLine();
file = new File(fileName);
if (file.isFile()) {
System.out.println(file + " 已存在!请重试:");
}
else {
break;
}
}
FileWriter writer = new FileWriter(file);
}
}
英文:
You shouldn't close a Scanner
that wraps System.in
since System.in
represents the standard input which is generally the computer keyboard. So when you close it, your program cannot receive input from the keyboard.
You should also not use exception handling to test a condition. You should use a conditional statement, like an if
statement.
In order to test whether a file exists, you can call method isFile. The method returns...
> true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise
When you create a FileWriter
, it also creates the file if it doesn't already exist.
Here is code demonstrating the above.
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class Cipher {
public static void main(String[] args) throws IOException {
System.out.println("Enter fileName: ");
Scanner input = new Scanner(System.in);
String fileName = null;
File file = null;
while (true) {
fileName = input.nextLine();
file = new File(fileName);
if (file.isFile()) {
System.out.println(file + " already exists! Please try again:");
}
else {
break;
}
}
FileWriter writer = new FileWriter(file);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论