2016-04-02 2 views
2

Как я могу убедиться, что этот метод закрывает OutputStream, так что утечки памяти не произойдет?Как я могу убедиться, что этот метод закрывает выходной поток?

public static void store(Properties properties, Class script) throws IOException { 
    ScriptManifest scriptManifest = (ScriptManifest) script.getAnnotation(ScriptManifest.class); 
    if (scriptManifest != null) { 
     String name = scriptManifest.name(); 
     FileOutputStream outputStream = new FileOutputStream(Constants.SCRIPT_PROPERTIES_DIR + File.separator + name + ".properties"); 
     properties.store(outputStream, ""); 
     outputStream.close(); 
    } else { 
     throw new RuntimeException("Script " + script.getName() + " does not have a ScriptManifest."); 
    } 
} 
+0

@OliverCharlesworth К сожалению, я Мента OutputStream. – user2997204

+0

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html –

ответ

2

Вы можете использовать try-with-resources. Пример:

public static void store(Properties properties, Class script) throws IOException { 
    ScriptManifest scriptManifest = (ScriptManifest) script.getAnnotation(ScriptManifest.class); 
    if (scriptManifest != null) { 
     String name = scriptManifest.name(); 
     try (FileOutputStream outputStream = new FileOutputStream(Constants.SCRIPT_PROPERTIES_DIR + File.separator + name + ".properties")) { 
      properties.store(outputStream, ""); 
     } 
    } else { 
     throw new RuntimeException("Script " + script.getName() + " does not have a ScriptManifest."); 
    } 
} 

или попробуйте, наконец, блокировать, как это:

public static void store(Properties properties, Class script) throws IOException { 
    ScriptManifest scriptManifest = (ScriptManifest) script.getAnnotation(ScriptManifest.class); 
    if (scriptManifest != null) { 
     String name = scriptManifest.name(); 
     FileOutputStream outputStream = null; 
     try { 
      outputStream = new FileOutputStream(Constants.SCRIPT_PROPERTIES_DIR + File.separator + name + ".properties"); 
      properties.store(outputStream, ""); 
     } finally { 
      if (outputStream != null) outputStream.close(); 
     } 
    } else { 
     throw new RuntimeException("Script " + script.getName() + " does not have a ScriptManifest."); 
    } 
} 
1

Существует два подхода.

FileOutputStream outputStream = null; 
try { 
    outputStream = new FileOutputStream(...) 
    ... 
} 
catch (IOException e) { 
    throw new RuntimeException(...) 
} 
finally { 
    // or use the Apache Commons IOUtils.closeQuietly(outputStream); 
    // and then only need the one line 
    if (outputStream != null) { 
     try { 
     outputStream.close(); 
     } 
     catch (Exception ignore) { } 
    } 
} 

В более поздних версиях Java, вы можете использовать примерочных с-ресурсы

try (FileOutputStream fos = new FileOutputStream("f:/tmp/stops.csv")) { 
} 
catch (IOException e) { 
} 
+0

можно ли это сделать без снятия бросков ....? – user2997204

+0

@ user2997204, в любом случае вы можете по-прежнему вызывать другое исключение (из IOException), если это необходимо. Я сделал редактирование в первом примере, чтобы проиллюстрировать. – KevinO

Смежные вопросы