2016-04-02 15 views
2

Bu yöntemin OutputStream'i kapattığından nasıl emin olabilirim, böylece bir bellek sızıntısı olmayacak?Bu yöntemin çıkış akışını kapattığından nasıl emin olabilirim?

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 Üzgünüm, OutputStream ment. – user2997204

+0

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

cevap

2

Java sonraki sürümlerinde

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) { } 
    } 
} 

, sen kullanabilirsiniz. Örnek:

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."); 
    } 
} 

veya nihayet böyle engellemek deneyin:

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

İki yaklaşım vardır. Sen try-with-resources kullanabilirsiniz deneyin-ile-kaynaklar

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

bu işlemi atmadan kaldırmanız mümkündür ....? – user2997204

+0

@ user2997204, her iki yaklaşımda da istenirse yine de farklı bir istisna (IOException'dan) atabilirsiniz. Örnek vermek için ilk örnekte bir düzenleme yaptım. – KevinO

İlgili konular