Java BufferedReader try 块错误
Java BufferedReader error with try block
我有一个应该加载文件的方法,但它给我这个错误:
NamnMetod.java:175: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
BufferedReader inFil = new BufferedReader(new FileReader(fil));
^
NamnMetod.java:177: error: unreported exception IOException; must be caught or declared to be thrown
String rad = inFil.readLine();
^
这是我的代码:
public static void hämtaFrånText() {
try {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
String aktuellMapp = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(aktuellMapp);
int resultat = fc.showOpenDialog(null);
if (resultat != JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, "Ingen fil valdes!");
System.exit(0);
}
String fil = fc.getSelectedFile().getAbsolutePath();
String[] namn = new String[3];
String output ="";
BufferedReader inFil = new BufferedReader(new FileReader(fil));
String rad = inFil.readLine();
int antal = 0;
while(rad != null){
namn[antal] = rad;
rad = inFil.readLine();
antal++;
}
inFil.close();
}
});
}catch(IOException e2) {
JOptionPane.showMessageDialog(null,"The file was not found!");
}
}
我很困惑,因为我同时捕获了 IOException 和 FileNotFoundException,但我仍然收到此错误...
您在创建 Runnable
的代码中捕获它们,而需要在 中捕获异常Runnable.run()
.
将 try/catch 移动到 run
方法中。
此外,使用 try-with-resources 确保 FileReader 始终关闭,即使发生异常:
try (FileReader fr = new FileReader(fil);
BufferedReader inFil = new BufferedReader(fr)) {
// ...
// No need to close `fr` or `inFil` explicitly.
}
解决方法如下:
public static void hämtaFrånText() {
try {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
String aktuellMapp = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(aktuellMapp);
int resultat = fc.showOpenDialog(null);
if (resultat != JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, "Ingen fil valdes!");
System.exit(0);
}
String fil = fc.getSelectedFile().getAbsolutePath();
String[] namn = new String[3];
String output = "";
BufferedReader inFil = new BufferedReader(new FileReader(fil));
String rad = inFil.readLine();
int antal = 0;
while (rad != null) {
namn[antal] = rad;
rad = inFil.readLine();
antal++;
}
inFil.close();
}catch(FileNotFoundException e1) {
JOptionPane.showMessageDialog(null,"The file was not found!");
}
catch(IOException e2) {
JOptionPane.showMessageDialog(null, "Failed!");
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
Reason
: 运行 是 Runnable 中的一个方法,在 eventQueue 中已经定义了它,在方法定义中没有处理异常的地方,也没有在方法声明语句中的 throw 中添加异常。因此 FileNoFoundException 和 IOException 都应该在方法内部而不是外部给出。
此外,InterruptedException 检查异常是由 EventQueue 抛出的,我添加了另一个 try catch 来处理该异常。
希望对您有所帮助。
你这里有范围问题。
您使用的 try 和 catch 将捕获 hämtaFrånText()
但不是方法 run()
的异常
异常可能发生在方法内部 run
而不是外部。
您提供的 try-Cathc 将只关心在其范围内发生的异常,因为您没有抛出这些异常,这不是您的 hämtaFrånText()
方法来处理 [=12= 内部发生的事情].
如果 run
是您定义的方法,那么您可以抛出它们。
但是现在你唯一的选择就是在 run()
方法中捕获它们。
所以试试
public void run() {
try{
.............
...........
}catch(FileNotFoundException e1) {
JOptionPane.showMessageDialog(null,"The file was not found!");
}
catch(IOException e2) {
JOptionPane.showMessageDialog(null, "Failed!");
}
}
try-catch 块属于 "Runnable" 的 运行()- 方法 - 此 运行()- 方法不得抛出任何异常。提交Runnable不会抛出异常
您需要将 try catch 块移动到 运行 方法中,这应该可以解决问题,您还需要处理 EventQueue.invokeAndWait(Runnable)
抛出的 InvocationTargetException
和 InterruptedException
正确编译你的class。
我有一个应该加载文件的方法,但它给我这个错误:
NamnMetod.java:175: error: unreported exception FileNotFoundException; must be caught or declared to be thrown
BufferedReader inFil = new BufferedReader(new FileReader(fil));
^
NamnMetod.java:177: error: unreported exception IOException; must be caught or declared to be thrown
String rad = inFil.readLine();
^
这是我的代码:
public static void hämtaFrånText() {
try {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
String aktuellMapp = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(aktuellMapp);
int resultat = fc.showOpenDialog(null);
if (resultat != JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, "Ingen fil valdes!");
System.exit(0);
}
String fil = fc.getSelectedFile().getAbsolutePath();
String[] namn = new String[3];
String output ="";
BufferedReader inFil = new BufferedReader(new FileReader(fil));
String rad = inFil.readLine();
int antal = 0;
while(rad != null){
namn[antal] = rad;
rad = inFil.readLine();
antal++;
}
inFil.close();
}
});
}catch(IOException e2) {
JOptionPane.showMessageDialog(null,"The file was not found!");
}
}
我很困惑,因为我同时捕获了 IOException 和 FileNotFoundException,但我仍然收到此错误...
您在创建 Runnable
的代码中捕获它们,而需要在 中捕获异常Runnable.run()
.
将 try/catch 移动到 run
方法中。
此外,使用 try-with-resources 确保 FileReader 始终关闭,即使发生异常:
try (FileReader fr = new FileReader(fil);
BufferedReader inFil = new BufferedReader(fr)) {
// ...
// No need to close `fr` or `inFil` explicitly.
}
解决方法如下:
public static void hämtaFrånText() {
try {
EventQueue.invokeAndWait(new Runnable() {
@Override
public void run() {
try {
String aktuellMapp = System.getProperty("user.dir");
JFileChooser fc = new JFileChooser(aktuellMapp);
int resultat = fc.showOpenDialog(null);
if (resultat != JFileChooser.APPROVE_OPTION) {
JOptionPane.showMessageDialog(null, "Ingen fil valdes!");
System.exit(0);
}
String fil = fc.getSelectedFile().getAbsolutePath();
String[] namn = new String[3];
String output = "";
BufferedReader inFil = new BufferedReader(new FileReader(fil));
String rad = inFil.readLine();
int antal = 0;
while (rad != null) {
namn[antal] = rad;
rad = inFil.readLine();
antal++;
}
inFil.close();
}catch(FileNotFoundException e1) {
JOptionPane.showMessageDialog(null,"The file was not found!");
}
catch(IOException e2) {
JOptionPane.showMessageDialog(null, "Failed!");
}
}
});
} catch (InterruptedException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
Reason
: 运行 是 Runnable 中的一个方法,在 eventQueue 中已经定义了它,在方法定义中没有处理异常的地方,也没有在方法声明语句中的 throw 中添加异常。因此 FileNoFoundException 和 IOException 都应该在方法内部而不是外部给出。
此外,InterruptedException 检查异常是由 EventQueue 抛出的,我添加了另一个 try catch 来处理该异常。
希望对您有所帮助。
你这里有范围问题。
您使用的 try 和 catch 将捕获 hämtaFrånText()
但不是方法 run()
异常可能发生在方法内部 run
而不是外部。
您提供的 try-Cathc 将只关心在其范围内发生的异常,因为您没有抛出这些异常,这不是您的 hämtaFrånText()
方法来处理 [=12= 内部发生的事情].
如果 run
是您定义的方法,那么您可以抛出它们。
但是现在你唯一的选择就是在 run()
方法中捕获它们。
所以试试
public void run() {
try{
.............
...........
}catch(FileNotFoundException e1) {
JOptionPane.showMessageDialog(null,"The file was not found!");
}
catch(IOException e2) {
JOptionPane.showMessageDialog(null, "Failed!");
}
}
try-catch 块属于 "Runnable" 的 运行()- 方法 - 此 运行()- 方法不得抛出任何异常。提交Runnable不会抛出异常
您需要将 try catch 块移动到 运行 方法中,这应该可以解决问题,您还需要处理 EventQueue.invokeAndWait(Runnable)
抛出的 InvocationTargetException
和 InterruptedException
正确编译你的class。