Java 什么是 IOException,我该如何解决?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52195112/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 00:22:17  来源:igfitidea点击:

What is a IOException, and how do I fix it?

javaioexception

提问by Victor

What are IO Exceptions (java.io.IOException) and what causes them?

什么是 IO 异常 (java.io.IOException) 以及导致它们的原因?

What methods/tools can be used to determine the cause so that you stop the exception from causing premature termination? What does this mean, and what can I do to fix this exception?

可以使用哪些方法/工具来确定原因,以便阻止异常导致提前终止?这是什么意思,我能做些什么来解决这个异常?

采纳答案by Code Daddy

Java IOExceptions are Input/Output exceptions (I/O), and they occur whenever an input or output operation is failed or interpreted. For example, if you are trying to read in a file that does not exist, Java would throw an I/O exception.

Java IOExceptions 是输入/输出异常 (I/O),它们在输入或输出操作失败或解释时发生。例如,如果您试图读入一个不存在的文件,Java 将抛出 I/O 异常。

When writing code that might throw an I/O exception, try writing the code in a try-catchblock. You can read more about them here: https://docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

编写可能引发 I/O 异常的代码时,请尝试将代码编写在try-catch块中。您可以在此处阅读有关它们的更多信息:https: //docs.oracle.com/javase/tutorial/essential/exceptions/catch.html

Your catch block should look something like this:

你的 catch 块应该是这样的:

try {
    //do something
}catch(FileNotFoundException ex){
    System.err.print("ERROR: File containing _______ information not found:\n");
    ex.printStackTrace();
    System.exit(1);
}

回答by mckuok

It is a very generic exception that a lot IO operation can cause. A best way is to read the Stack Trace. To continue the execution you can use the try-catchblock to bypass the exception, but as you mention you should investigate into the cause.

这是一个非常普遍的异常,很多 IO 操作都会导致。最好的方法是阅读堆栈跟踪。要继续执行,您可以使用该try-catch块来绕过异常,但正如您所提到的,您应该调查原因。

To print the stack trace:

要打印堆栈跟踪:

try {
    // IO operation that could cause an exception
} catch (Exception ex) {
    ex.printStackTrace();
}

回答by Stef

Here you go https://docs.oracle.com/javase/7/docs/api/java/io/IOException.html

给你https://docs.oracle.com/javase/7/docs/api/java/io/IOException.html

IOExceptionis thrown when an error occurred during an input-output operation. That can be reading/writing to a file, a stream (of any type), a network connection, connection with a queue, a database etc, pretty much anything that has to do with data transfer from your software to an external medium.

IOException在输入-输出操作期间发生错误时抛出。这可以是读/写文件、流(任何类型)、网络连接、与队列的连接、数据库等,几乎所有与从软件到外部介质的数据传输有关的事情。

In order to fix it, you would want to see the stack trace of your exception or at least the message, to see exactly where the exception is thrown and why.

为了修复它,您需要查看异常的堆栈跟踪或至少是消息,以准确了解抛出异常的位置以及原因。

try {
    methodThrowingIOException();
} catch (IOException e) {
    System.out.println(e.getMessage()); //if you're using a logger, you can use that instead to print.
    //e.printStackTrace(); //or print the full stack.
}

The error message that will be printed will likely show you what the issue is. If you add the error message here, I'll be able to give you more info on how to fix that specific IOException. Without that, no one can really give you a complete answer.

将打印的错误消息可能会向您显示问题所在。如果您在此处添加错误消息,我将能够为您提供有关如何修复该特定 IOException 的更多信息。没有那个,没有人能真正给你一个完整的答案。

回答by kPs

IOException is usually a case in which the user inputs improper data into the program. This could be data types that the program can't handle or the name of a file that doesn't exist. When this happens, an exception (IOException) occurs telling the compiler that invalid input or invalid output has occurred.

IOException 通常是用户在程序中输入了不正确的数据的情况。这可能是程序无法处理的数据类型或不存在的文件名。发生这种情况时,会发生异常 (IOException),告诉编译器发生了无效输入或无效输出。

Like others have said, you can use a try-catch statement to stop a premature termination.

就像其他人所说的那样,您可以使用 try-catch 语句来阻止过早终止。

try {
 // Body of code
} catch (IOException e) {
 e.printStackTrace();
}