銆愯浆銆慾ava.io.Closeable鎺ュ彛

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了銆愯浆銆慾ava.io.Closeable鎺ュ彛相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/row' title='row'>row   system   print   鍑虹幇   寮曞叆   鎶涘嚭寮傚父   鏂规硶   璋冪敤   

璇村埌java.io.Closeable鎺ュ彛灏遍伩涓嶅紑java.lang.AutoCloseable鎺ュ彛锛屽洜涓哄湪java鐗堟湰7.0鏃跺紩鍏ヤ簡java.lang.AutoCloseable鎺ュ彛锛屽悓鏃秊ava.io.Closeable鎺ュ彛渚跨户鎵胯嚜java.lang.AutoCloseable鎺ュ彛浜嗐€?/p>

java.io.Closeable

鍏堣涓€涓婥loseable鎺ュ彛锛岃繖涓帴鍙d粠java 5.0鐗堟湰寮€濮嬪紩鍏ワ紝鍏朵腑涓粎澹版槑浜嗕竴涓柟娉昪lose锛岀敤浜庡叧闂竴涓祫婧愩€備竴鐩翠竴鏉ユ垜閮藉緢鍥版儜锛屽氨绠椾笉瀹炵幇杩欎釜鎺ュ彛锛屾垜缁欐垜鐨勭被瀹炵幇涓€涓猚lose鏂规硶锛堟垨鑰呭埆鐨勬柟娉曪級鏉ュ畬鎴?ldquo;鍏抽棴”鐨勫姛鑳戒笉涔熸槸涓€鏍风殑涔堛€傜洿鍒版垜鐪嬪埌涓嬮潰鐨勪袱娈典唬鐮併€?/p>

//绗竴娈典唬鐮?
static void copy(String src, String dest)throws IOException {
  InputStream in = null;
  OutputStream out = null; 
  try {
    in = new FileInputStream(src);
    out = new FileOutputStream(dest);
    byte[] buf = new byte[1024];
    int n;
    while ((n = in.read(buf)) >= 0) {
      out.write(buf, 0, n);
    }
  } finally {
    if (in != null) in.close();
    if (out != null) out.close();
  }
}

涓婇潰杩欐浠g爜鐨勯棶棰樺湪浜庯紝finally璇彞涓殑close鏂规硶涔熷彲鑳戒細鎶涘嚭IOException寮傚父銆傚鏋滆繖姝eソ鍙戠幇鍦╥n.close琚皟鐢ㄤ箣鏃讹紝閭d箞杩欎釜寮傚父灏变細闃绘out.close琚皟鐢紝浠庤€屼娇杈撳嚭娴佷繚鎸佸湪鎵撳紑鐘舵€併€傛墍浠ヨ繖涓▼搴忎娇寰梖inally鍙兘琚剰澶栫粨鏉熴€傝В鍐虫柟寮忔槸灏嗘瘡涓€涓猚lose閮藉寘瑁呭湪涓€涓猼ry璇彞鍧椾腑銆備粠java 5.0鐗堟湰寮€濮嬶紝鍙互鍒╃敤Closeable鎺ュ彛銆?/p>

//绗簩娈典唬鐮?
// 瀵圭涓€娈典唬鐮佷腑鐨刦inally璇彞鏀归€犲涓?
finally {
  closeIgnoringIOException(in);
  closeIgnoringIOException(out);
}
private static void closeIgnoringIOException(Closeable c) {
  if (c != null) {
    try {
      c.close();
    } catch (IOException ex) { }
  }
}

鍩轰簬浠ヤ笂涓ゆ浠g爜鎴戠煡閬撲簡java.io.Closeable鎺ュ彛鐨勭敤澶勩€?/p>

java.lang.AutoCloseable

鍦╦ava 7.0j鏃跺紩鍏ヤ簡java.lang.AutoCloseable锛屽苟涓攋ava.io.Closeable鎺ュ彛缁ф壙鑷?java.lang.AutoCloseable銆傚緢澶氳祫婧愮被閮界洿鎺ユ垨闂存帴鐨勫疄鐜颁簡姝ゆ帴鍙c€傚叾瀹炶繖涓帴鍙d笌try-with-resources璇硶鏄瘑鍒囩浉鍏崇殑銆?/p>

浠嶢utoCloseable鐨勬敞閲婂彲鐭ュ畠鐨勫嚭鐜版槸涓轰簡鏇村ソ鐨勭鐞嗚祫婧愶紝鍑嗙‘璇存槸璧勬簮鐨勯噴鏀撅紝褰撲竴涓祫婧愮被瀹炵幇浜嗚鎺ュ彛close鏂规硶锛屽湪浣跨敤try-with-resources璇硶鍒涘缓鐨勮祫婧愭姏鍑哄紓甯稿悗锛孞VM浼氳嚜鍔ㄨ皟鐢╟lose 鏂规硶杩涜璧勬簮閲婃斁锛屽綋娌℃湁鎶涘嚭寮傚父姝e父閫€鍑簍ry-block鏃跺€欎篃浼氳皟鐢╟lose鏂规硶銆?/p>

//绗笁娈典唬鐮?
static void copy(String src, String dest)throws IOException {
  try (InputStream in=new FileInputStream(src);OutputStream out=new FileOutputStream(dest)){
    byte[] buf = new byte[1024];
    int n;
    while ((n = in.read(buf)) >= 0) {
      out.write(buf, 0, n);
    }
  }catch(Exception e) {
    System.out.println("catch block:"+e.getLocalizedMessage());
  }finally{
    System.out.println("finally block");
  }
}

鍥犱负InputStream鍜孫utputStream閮藉疄鐜颁簡java.io.Closeable鎺ュ彛锛堥棿鎺ュ疄鐜颁簡java.lang.AutoCloseable鎺ュ彛锛夋墍浠ヤ笂闈㈢殑銆愮涓夋浠g爜銆戜笌銆愮浜屾浠g爜鐨勩€戜竴鏍?ldquo;瀹夊叏”銆?/p>

try-with-resources

try-with-resources 鏄湪java 7.0 鐗堟湰鏃跺紩鍏ョ殑鏂拌娉曠壒鎬с€備娇鐢ㄥ畠閰嶅悎java.lang.AutoCloseable鎺ュ彛鍙互鏇村ソ鐨勫璧勬簮杩涜閲婃斁锛屽噺灏戠箒鐞愮殑寮傚父澶勭悊銆?/p>

  1. 浣跨敤try-with-resources缁撴瀯鏃犺鏄惁鎶涘嚭寮傚父鍦╰ry-block鎵ц瀹屾瘯鍚庨兘浼氳皟鐢ㄨ祫婧愮殑close鏂规硶锛?/li>
  2. 浣跨敤try-with-resources缁撴瀯鍒涘缓澶氫釜璧勬簮锛宼ry-block鎵ц瀹屾瘯鍚庤皟鐢ㄧ殑close鏂规硶鐨勯『搴忎笌鍒涘缓璧勬簮椤哄簭鐩稿弽锛?/li>
  3. 浣跨敤try-with-resources缁撴瀯锛宼ry-block鍧楁姏鍑哄紓甯稿悗鍏堟墽琛屾墍鏈夎祫婧愶紙try鐨勶紙锛変腑澹版槑鐨勶級鐨刢lose鏂规硶鐒跺悗鍦ㄦ墽琛宑atch閲岄潰鐨勪唬鐮佺劧鍚庢墠鏄痜inally锛?/li>
  4. 鍙敤鍦╰ry鐨?)涓0鏄庣殑璧勬簮鐨刢lose鏂规硶鎵嶄細琚皟鐢紝骞朵笖褰撳璞¢攢姣佺殑鏃跺€檆lose涓嶄細琚啀娆¤皟鐢紱
  5. 浣跨敤try-with-resources缁撴瀯鏃犻』鏄惧紡璋冪敤璧勬簮閲婃斁锛岀紪绋嬫晥鐜囬珮锛屼唬鐮佹洿绠€娲併€?/li>

以上是关于銆愯浆銆慾ava.io.Closeable鎺ュ彛的主要内容,如果未能解决你的问题,请参考以下文章

銆愯浆銆戙€怱alesfoece銆慉pproval Process 鍦?Apex 涓殑浣跨敤

銆愯浆銆?springboot鏁村悎redis-sentinel鏀寔Cache娉ㄨВ

銆愯浆銆戝悓婧愮瓥鐣ュ拰璺ㄥ煙璇锋眰瑙e喅鏂规

銆愯浆銆慔Base涓璟ookeeper锛孯egionServer锛孧aster锛孋lient涔嬮棿鍏崇郴

Dubbo鎺ュ彛涓嶇煡閬撴€庝箞娴嬶紵鏁欎綘涓夎浠g爜瀹炵幇dubbo鎺ュ彛娴嬭瘯

鎺ュ彛鏌ヨ鏂规硶