銆愯浆銆慾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>
- 浣跨敤try-with-resources缁撴瀯鏃犺鏄惁鎶涘嚭寮傚父鍦╰ry-block鎵ц瀹屾瘯鍚庨兘浼氳皟鐢ㄨ祫婧愮殑close鏂规硶锛?/li>
- 浣跨敤try-with-resources缁撴瀯鍒涘缓澶氫釜璧勬簮锛宼ry-block鎵ц瀹屾瘯鍚庤皟鐢ㄧ殑close鏂规硶鐨勯『搴忎笌鍒涘缓璧勬簮椤哄簭鐩稿弽锛?/li>
- 浣跨敤try-with-resources缁撴瀯锛宼ry-block鍧楁姏鍑哄紓甯稿悗鍏堟墽琛屾墍鏈夎祫婧愶紙try鐨勶紙锛変腑澹版槑鐨勶級鐨刢lose鏂规硶鐒跺悗鍦ㄦ墽琛宑atch閲岄潰鐨勪唬鐮佺劧鍚庢墠鏄痜inally锛?/li>
- 鍙敤鍦╰ry鐨?)涓0鏄庣殑璧勬簮鐨刢lose鏂规硶鎵嶄細琚皟鐢紝骞朵笖褰撳璞¢攢姣佺殑鏃跺€檆lose涓嶄細琚啀娆¤皟鐢紱
- 浣跨敤try-with-resources缁撴瀯鏃犻』鏄惧紡璋冪敤璧勬簮閲婃斁锛岀紪绋嬫晥鐜囬珮锛屼唬鐮佹洿绠€娲併€?/li>
以上是关于銆愯浆銆慾ava.io.Closeable鎺ュ彛的主要内容,如果未能解决你的问题,请参考以下文章
銆愯浆銆戙€怱alesfoece銆慉pproval Process 鍦?Apex 涓殑浣跨敤
銆愯浆銆?springboot鏁村悎redis-sentinel鏀寔Cache娉ㄨВ
銆愯浆銆慔Base涓璟ookeeper锛孯egionServer锛孧aster锛孋lient涔嬮棿鍏崇郴