因为我是网站新手,请在这里忍受。 下面是我为Java编程编写的程序,虽然目前大部分程序都运行良好,但我似乎无法摆脱特定的错误。
当程序到达第三个if块(选项== 3)时,它不会让用户输入任何数据,如果该行
“outputStream = openOutputTextFile(newerFileName);”
出现在if块中,然后发生FileNotFoundException。在修改了我的代码一段时间后,我发现错误被抛出,因为程序无法再找到inputStream。虽然我已经检查过并且发现该程序仍然可以查找,读取和写入导致错误的文件。
我在想,因为错误只发生在我将outputStream放入并且被inputStream抛出时,它可能与文件流有关。我只是不知道究竟是什么
有没有人对如何解决这个问题有任何想法?
public class FileProgram {
public static PrintWriter openOutputTextFile(String fileName)
throws FileNotFoundException {
PrintWriter toFile = new PrintWriter(fileName);
return toFile;
}
public static Scanner readFile(String fileName)
throws FileNotFoundException {
Scanner inputStream = new Scanner(new File(fileName));
return inputStream;
}
public static void main(String args[]) throws FileNotFoundException {
ArrayList<String>fileReader = new ArrayList<String>(10);
PrintWriter outputStream = null;
Scanner inputStream = null;
Scanner keyboard = new Scanner(System.in);
try {
System.out.println("Enter the name of the text file you want to copy.");
String oldFileName = keyboard.nextLine();
inputStream = readFile(oldFileName);
while(inputStream.hasNextLine()) {
String currentLine = inputStream.nextLine();
fileReader.add(currentLine);
}
System.out.println("All data has been collected. Enter the name for the new text file");
String newFileName = keyboard.nextLine();
outputStream = openOutputTextFile(newFileName);
File userFile = new File(newFileName);
if(userFile.exists())
{
System.out.println("The name you entered matches a file that already exists.");
System.out.println("Here are your options to fix this issue.");
System.out.println("Option 1: Shut down the program.");
System.out.println("Option 2: Overwrite the old file with the new empty one.");
System.out.println("Option 3: Enter a different name for the new file.");
System.out.println("Enter the number for the option that you want.");
int choice = keyboard.nextInt();
if(choice == 1) {
System.exit(0);
} else if(choice == 2) {
outputStream = new PrintWriter(newFileName);
} **else if(choice == 3) {
System.out.println("Enter a different name.");
String newerFileName = keyboard.nextLine();
outputStream = openOutputTextFile(newerFileName);
}**
}
for(int i = 0; i < fileReader.size(); i++) {
String currentLine = fileReader.get(i);
outputStream.println(currentLine);
//System.out.println(currentLine);
}
System.out.println("The old file has been copied line-by-line to the new file.");
}
catch(FileNotFoundException e) {
System.out.println("File not found");
System.out.println("Shutting program down.");
System.exit(0);
}
finally {
outputStream.close();
inputStream.close();
}
}
}
答案 0 :(得分:2)
在调用.nextInt()后,您无法从Scanner对象获取一行输入。在响应数字选择时,用户输入一个整数,后跟换行符。
该行从输入缓冲区中读取整数:
int choice = keyboard.nextInt();
但是,在数字后面的输入缓冲区中仍然有换行符。因此,当您调用.nextLine()时:
String oldFileName = keyboard.nextLine();
你得到一个空行。您无法为文件名创建带有空字符串的文件,因此会抛出FileNotFoundException(这是每个规范,请参阅其他答案)。
一种解决方案是始终使用.nextLine(),从输入缓冲区一次获取一行。当您需要整数时,只需手动解析字符串:
int choice = Integer.parseInt( keyboard.nextLine() );
顺便说一句,在调试这类问题时,养成添加一些打印输出语句以查看正在发生的事情的习惯是非常有用的:
public static PrintWriter openOutputTextFile(String fileName)
throws FileNotFoundException {
System.out.println( "Trying to create file: '" + fileName + "'" );
PrintWriter toFile = new PrintWriter(fileName);
return toFile;
}
还有更高级的调试技术,但这个技术非常简单,使用它比使用任何东西都要好得多。