当我想关闭InputFileStream和OutputFileStream对象时,eclipse说我需要捕获IOException,因此在捕获这些异常之后这是我的代码。 正如您所看到的,我正在捕获两次IOException。 是否有一种更简单的方法,我只能有一个块来捕获in.close()和in.read()的IOException?
public class ByteStream { public static void main(String[] args) { FileInputStream in = null; try { in = new FileInputStream("testdata.txt"); int nextByte; while((nextByte = in.read()) != -1){ System.out.println(nextByte + "-"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null){ try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
When I want to close InputFileStream and OutputFileStream objects, eclipse says that I need to catch IOException so here is my code after catching those exceptions. As you can see I am catching IOException twice. Is there a more simple way that I can have only one block for catching IOException for both in.close() and in.read() ?
public class ByteStream { public static void main(String[] args) { FileInputStream in = null; try { in = new FileInputStream("testdata.txt"); int nextByte; while((nextByte = in.read()) != -1){ System.out.println(nextByte + "-"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null){ try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
最满意答案
在Java 7中使用try-with-resources语法
try (FileInputStream in = new FileInputStream("testdata.txt");){ int nextByte; while ((nextByte = in.read()) != -1) { System.out.println(nextByte + "-"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }编译器将负责将上面的代码转换为关闭InputStream代码,或者在try表达式的()部分中声明和实例化的任何其他AutoCloseable对象。
Use the try-with-resources syntax in Java 7
try (FileInputStream in = new FileInputStream("testdata.txt");){ int nextByte; while ((nextByte = in.read()) != -1) { System.out.println(nextByte + "-"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }the compiler will take care of converting the above code to code that closes the in InputStream, or any other AutoCloseable object declared and instatiated in the () part of the try expression.
关闭FileInputStream对象会抛出异常(closing FileInputStream object throws exception)当我想关闭InputFileStream和OutputFileStream对象时,eclipse说我需要捕获IOException,因此在捕获这些异常之后这是我的代码。 正如您所看到的,我正在捕获两次IOException。 是否有一种更简单的方法,我只能有一个块来捕获in.close()和in.read()的IOException?
public class ByteStream { public static void main(String[] args) { FileInputStream in = null; try { in = new FileInputStream("testdata.txt"); int nextByte; while((nextByte = in.read()) != -1){ System.out.println(nextByte + "-"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null){ try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
When I want to close InputFileStream and OutputFileStream objects, eclipse says that I need to catch IOException so here is my code after catching those exceptions. As you can see I am catching IOException twice. Is there a more simple way that I can have only one block for catching IOException for both in.close() and in.read() ?
public class ByteStream { public static void main(String[] args) { FileInputStream in = null; try { in = new FileInputStream("testdata.txt"); int nextByte; while((nextByte = in.read()) != -1){ System.out.println(nextByte + "-"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in != null){ try { in.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }}
最满意答案
在Java 7中使用try-with-resources语法
try (FileInputStream in = new FileInputStream("testdata.txt");){ int nextByte; while ((nextByte = in.read()) != -1) { System.out.println(nextByte + "-"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }编译器将负责将上面的代码转换为关闭InputStream代码,或者在try表达式的()部分中声明和实例化的任何其他AutoCloseable对象。
Use the try-with-resources syntax in Java 7
try (FileInputStream in = new FileInputStream("testdata.txt");){ int nextByte; while ((nextByte = in.read()) != -1) { System.out.println(nextByte + "-"); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }the compiler will take care of converting the above code to code that closes the in InputStream, or any other AutoCloseable object declared and instatiated in the () part of the try expression.
发布评论