在 Java 中处理 IO 异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6250231/
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
Handling IO exceptions in Java
提问by fredoverflow
Basically, I want to open a file, read some bytes, and then close the file. This is what I came up with:
基本上,我想打开一个文件,读取一些字节,然后关闭文件。这就是我想出的:
try
{
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
try
{
// ...
inputStream.read(buffer);
// ...
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try
{
inputStream.close();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
Maybe I'm spoiled by RAII, but there must be a better way to do this in Java, right?
也许我被 RAII 宠坏了,但是在 Java 中肯定有更好的方法来做到这一点,对吧?
采纳答案by vitaut
If you have the same exception handling code for IOException
and FileNotFoundException
then you can rewrite your example in a more compact way with only one catch
clause:
如果您有相同的异常处理代码IOException
,FileNotFoundException
那么您可以只用一个catch
子句以更紧凑的方式重写您的示例:
try {
InputStream input = new BufferedInputStream(new FileInputStream(file));
try {
// ...
input.read(buffer);
// ...
}
finally {
input.close();
}
}
catch (IOException e) {
e.printStackTrace();
}
You can even get rid of the outer try-catch
if you can propagate the exception which probably makes more sense then manually printing the stack trace. If you don't catch some exception in your program you'll get stack trace printed for you automatically.
try-catch
如果您可以传播异常,您甚至可以摆脱外部,这可能比手动打印堆栈跟踪更有意义。如果您没有在程序中捕获某些异常,则会自动为您打印堆栈跟踪。
Also the need to manually close the stream will be addressed in Java 7 with automatic resource management.
此外,手动关闭流的需要将在 Java 7 中通过自动资源管理解决。
With automatic resource management and exception propagation the code reduces to the following:
通过自动资源管理和异常传播,代码简化为以下内容:
try (InputStream input = new BufferedInputStream(new FileInputStream(file))) {
// ...
input.read(buffer);
// ...
}
回答by Peter Lawrey
Usually these methods are wrapped up in libraries. Unless you want to write at this level, it is best to create your own helper methods or use existing ones like FileUtils.
通常这些方法都包含在库中。除非您想在此级别编写,否则最好创建自己的辅助方法或使用现有的方法,例如FileUtils。
String fileAsString = Fileutils.readFileToString(filename);
// OR
for(String line: FileUtils.readLines(filename)) {
// do something with each line.
}
回答by musiKk
Sometimes you can reduce the code to the following:
有时您可以将代码简化为以下内容:
public void foo(String name) throws IOException {
InputStream in = null;
try {
in = new FileInputStream(name);
in.read();
// whatever
} finally {
if(in != null) {
in.close();
}
}
}
Of course this means the caller of foo
has to handle the IOException
but this should be the case most of the time anyway. In the end you don't really reduce all that much complexity but the code becomes much more readable due to less nested exception handlers.
当然,这意味着 的调用者foo
必须处理 ,IOException
但无论如何大多数情况下都应该是这种情况。最后,您并没有真正降低那么多复杂性,但是由于嵌套的异常处理程序更少,代码变得更具可读性。
回答by gpeche
My take on this without using utilities would be:
我在不使用实用程序的情况下对此的看法是:
InputStream inputStream = null;
try {
inputStream = new BufferedInputStream(new FileInputStream(file));
// ...
inputStream.read(buffer);
// ...
} catch (IOException e) {
e.printStackTrace();
throw e; // Rethrow if you cannot handle the exception
} finally {
if (inputStream != null) {
inputStream.close();
}
}
Not a one-liner, but not pretty bad. Using, say, Apache Commons IO it would be:
不是单线,但还不错。例如,使用 Apache Commons IO 将是:
//...
buffer = FileUtils.readFileToByteArray(file);
//...
The thing to rembember is that standard Java lacks many of these little utilities and easy to use interfaces that everyone needs, so you have to rely on some support libraries like Apache Commons, Google Guava, ... in your projects, (or implement your own utility classes).
需要记住的是,标准 Java 缺少每个人都需要的许多这些小实用程序和易于使用的接口,因此您必须依赖一些支持库,例如Apache Commons、Google Guava……在您的项目中,(或实现您的自己的实用程序类)。
回答by Rasmus ?vlesen
I don't know if it is the right way, but you can have all your code in the same try block, and then have the different catch blocks right after each other.
我不知道这是否是正确的方法,但是您可以将所有代码放在同一个 try 块中,然后将不同的 catch 块紧接其后。
try {
...
}
catch (SomeException e) {
...
}
catch (OtherException e) {
...
}
回答by Dave
If you want to do this in plain Java, the code you have posted looks OK.
如果你想用普通的 Java 来做这件事,你发布的代码看起来没问题。
You could use 3rd party libraries such as Commons IO, in which you would need to write much less code. e.g.
您可以使用 3rd 方库,例如 Commons IO,您需要在其中编写更少的代码。例如
Check out Commons IO at:
在以下位置查看 Commons IO:
回答by artbristol
Use org.apache.commons.io.FileUtils.readFileToByteArray(File)
or something similar from that package. It still throws IOException but it deals with cleaning up for you.
使用org.apache.commons.io.FileUtils.readFileToByteArray(File)
该包中的或类似的东西。它仍然会抛出 IOException 但它会为你清理。
回答by Hasan Fahim
Try the following:
请尝试以下操作:
try
{
InputStream inputStream = new BufferedInputStream(new FileInputStream(file));
byte[] buffer = new byte[1024];
try
{
// ...
int bytesRead = 0;
while ((bytesRead = inputStream.read(buffer)) != -1) {
//Process the chunk of bytes read
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
inputStream.close();
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
回答by mindas
Google guava has tried to address this problem by introducing Closeables.
谷歌番石榴试图通过引入Closeables来解决这个问题。
Otherwise you have to wait until AutoCloseablefrom JDK 7 comes out as it addresses some of the cases when IOException is thrown.
否则,您必须等到JDK 7 的AutoCloseable出现,因为它解决了抛出 IOException 的一些情况。