Java 使用BufferedReader和FileReader;文件正在读取但未输出其内容?
我已经编写了一个程序,应该使用Java 使用BufferedReader和FileReader;文件正在读取但未输出其内容?,java,bufferedreader,filereader,Java,Bufferedreader,Filereader,我已经编写了一个程序,应该使用BufferedReader和FileReader类读取外部文件。它可以识别文件并成功构建,但不会打印出文本文件的内容。代码如下: 节目 import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.Scanner; public class La
BufferedReader
和FileReader
类读取外部文件。它可以识别文件并成功构建,但不会打印出文本文件的内容。代码如下:
节目
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
public class Lab9 {
public static void main(String[] args) {
BufferedReader reader = null;
String line;
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a file name to read");
try {
reader = new BufferedReader(new FileReader("C:\\Users\\Lee\\Documents\\NetBeansProjects\\Lab9\\" + sc.next()));
} catch (FileNotFoundException ex) {
System.out.println(ex.getMessage() + "File was not found");
try {
while ((line = reader.readLine()) != null)
{
System.out.println(line);
}
} catch (IOException ex2) {
System.out.println(ex2.getMessage() + "File did not read correctly");
} finally {
System.exit(0);
}
}
}
}
应打印的文件内容如下所示:
public static void main(String[] args) {
BufferedReader reader = null;
String line;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a file name to read: ");
try {
reader = new BufferedReader(
new FileReader("C:\\Users\\Lee\\Documents\\NetBeansProjects\\Lab9\\" + sc.next()));
try {
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
System.out.println(
ex.getMessage() + "File did not read correctly");
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (FileNotFoundException ex) {
System.out.println("File was not found.");
}
}
文件内容
By what initials was Franklin Roosevelt better known?:FDR
Which number president was Franklin Roosevelt?:32
Which state was Franklin Roosevelt born in?:New York
In which year did Roosevelt become Governor of New York?:1929
What was the name of Franklin Roosevelt's wife?:Eleanor
How many children did Franklin Roosevelt have?:6
From which university did Franklin Roosevelt graduate with an A.B in history?:Harvard
What was the first name of Franklin Roosevelt's 5th cousin, who was also President?:Theodore
Which disease is believed to be the causes of Franklin Roosevelt's paralysis?:Polio
At what age did Franklin Roosevelt die?:63
实际产出
Please enter a file name to read
Questions.txt
BUILD SUCCESSFUL (total time: 6 seconds)
非常感谢您对解决此问题的任何帮助。您已尝试在异常分支中读取
文件NotFoundException
的文件。这意味着,只有在找不到文件时,程序才会读取该文件,这是没有意义的。它的工作原理如下:
public static void main(String[] args) {
BufferedReader reader = null;
String line;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a file name to read: ");
try {
reader = new BufferedReader(
new FileReader("C:\\Users\\Lee\\Documents\\NetBeansProjects\\Lab9\\" + sc.next()));
try {
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException ex) {
System.out.println(
ex.getMessage() + "File did not read correctly");
} finally {
try {
if (reader != null)
reader.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
} catch (FileNotFoundException ex) {
System.out.println("File was not found.");
}
}
作为旁注,您应该始终关闭所使用的资源,在这种情况下,您的文件是由
FileReader
打开的,如上面的finally
分支所示。此外,您不需要调用System.exit(0)
第一个catch语句没有结束括号,因此文件读取器位于catch语句中。您仅在抛出FileNotFoundException
时才尝试读取文件。您还应该在使用资源后关闭它们。这个怎么样
public static void main(String[] args) throws IOException {
String line;
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter a file name to read");
try (BufferedReader reader = new BufferedReader(new FileReader("C:\\Users\\Lee\\Documents\\NetBeansProjects\\Lab9\\" + scanner.next()))) {
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (FileNotFoundException fileNotFoundException) {
System.out.println(fileNotFoundException.getMessage() + ". File was not found");
}
}
这段代码编译了吗?你的第一个挡块没有关上。提示:在IDE中正确设置所有代码的格式,这样缩进将对您有所帮助。然后查看
while
循环的位置。我建议删除所有try/catch语句,并声明您的main
方法可以抛出IOException
…我将其中一个括号放错了位置。经过缩进并正确格式化后,我发现了错误,谢谢。