最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java之如何關(guān)閉流

 更新時間:2022年11月22日 09:08:52   作者:思想永無止境  
這篇文章主要介紹了Java之如何關(guān)閉流問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

我們深知在操作Java流對象后要將流關(guān)閉,但往往事情不盡人意,大致有以下幾種不能一定將流關(guān)閉的寫法:

1.在try中關(guān)流,而沒在finally中關(guān)流

try {
	OutputStream out = new FileOutputStream("");
	// ...操作流代碼
	out.close();
} catch (Exception e) {
	e.printStackTrace();
}

正確寫法:

OutputStream out = null;
try {
	out = new FileOutputStream("");
	// ...操作流代碼
} catch (Exception e) {
	e.printStackTrace();
} finally {
	try {
		if (out != null) {
			out.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}

2.在關(guān)閉多個流時因為嫌麻煩將所有關(guān)流的代碼丟到一個try中

OutputStream out = null;
OutputStream out2 = null;
try {
	out = new FileOutputStream("");
	out2 = new FileOutputStream("");
	// ...操作流代碼
} catch (Exception e) {
	e.printStackTrace();
} finally {
	try {
		if (out != null) {
			out.close();// 如果此處出現(xiàn)異常,則out2流沒有被關(guān)閉
		}
		if (out2 != null) {
			out2.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}

正確寫法:

OutputStream out = null;
OutputStream out2 = null;
try {
	out = new FileOutputStream("");
	out2 = new FileOutputStream("");
	// ...操作流代碼
} catch (Exception e) {
	e.printStackTrace();
} finally {
	try {
		if (out != null) {
			out.close();// 如果此處出現(xiàn)異常,則out2流也會被關(guān)閉
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
	try {
		if (out2 != null) {
			out2.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}

3.在循環(huán)中創(chuàng)建流

在循環(huán)外關(guān)閉,導致關(guān)閉的是最后一個流

OutputStream out = null;
try {
	for (int i = 0; i < 10; i++) {
		out = new FileOutputStream("");
		// ...操作流代碼
	}
} catch (Exception e) {
	e.printStackTrace();
} finally {
	try {
		if (out != null) {
			out.close();
		}
	} catch (Exception e) {
		e.printStackTrace();
	}
}

正確寫法:

for (int i = 0; i < 10; i++) {
	OutputStream out = null;
	try {
		out = new FileOutputStream("");
		// ...操作流代碼
	} catch (Exception e) {
		e.printStackTrace();
	} finally {
		try {
			if (out != null) {
				out.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

4.在Java7中

關(guān)閉流這種繁瑣的事情再也不用我們自己敲代碼了

try (OutputStream out = new FileOutputStream("")){
	// ...操作流代碼
} catch (Exception e) {
	e.printStackTrace();
}

只要實現(xiàn)的自動關(guān)閉接口(Closeable)的類都可以在try結(jié)構(gòu)體上定義,java會自動幫我們關(guān)閉,及時在發(fā)生異常的情況下也會??梢栽趖ry結(jié)構(gòu)體上定義多個,用分號隔開即可,如:

try (OutputStream out = new FileOutputStream("");OutputStream out2 = new FileOutputStream("")){
	// ...操作流代碼
} catch (Exception e) {
	throw e;
}

注:Android SDK 20版本對應java7,低于20版本無法使用try-catch-resources自動關(guān)流。

5.內(nèi)存流可以不用關(guān)閉(關(guān)與不關(guān)都可以,沒影響)

ByteArrayOutputStream和ByteArrayInputStream其實是偽裝成流的字節(jié)數(shù)組(把它們當成字節(jié)數(shù)據(jù)來看就好了),他們不會鎖定任何文件句柄和端口,如果不再被使用,字節(jié)數(shù)組會被垃圾回收掉,所以不需要關(guān)閉。

6.使用裝飾流時,只需要關(guān)閉最后面的裝飾流即可

裝飾流是指通過裝飾模式實現(xiàn)的java流,又稱為包裝流,裝飾流只是為原生流附加額外的功能(或效果),java中的緩沖流、橋接流也是屬于裝飾流。

        InputStream is=new FileInputStream("C:\\Users\\tang\\Desktop\\記事本.txt");
		InputStreamReader isr=new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		String string = br.readLine();
		System.out.println(string);
		try {
			br.close();//只需要關(guān)閉最后的br即可
		} catch (Exception e) {
			e.printStackTrace();
		}

裝飾流關(guān)閉時會調(diào)用原生流關(guān)閉,請看源碼:

   //BufferedReader.java
    public void close() throws IOException {
        synchronized (lock) {
            if (in == null)
                return;
            try {
                in.close();//這里的in就是原生流
            } finally {
                in = null;
                cb = null;
            }
        }
    }
//InputStreamReader.java
public void close() throws IOException {
        sd.close();//這里的sd就是原生流的解碼器(StreamDecoder),解碼器的close會調(diào)用原生流的close
    }

有這樣層層關(guān)閉的機制,我們就只需要關(guān)閉最外層的流就行了(甚至博主認為,其實只關(guān)閉最里層的流也可以,因為裝飾流并不持有任何文件句柄和端口,它和內(nèi)存流一樣是個“假流”,當然這只是我的個人推理,不敢保證只關(guān)閉最里層的流一定沒有問題)。

7.關(guān)閉流時的順序問題

兩個不相干的流的關(guān)閉順序沒有任何影響,如:

out1 = new FileOutputStream("");
out2 = new FileOutputStream("");
//這里的out1和out2誰先關(guān)誰后關(guān)都一樣,沒有任何影響

如果兩個流有依賴關(guān)系,那么你可以像上面說的,只關(guān)閉最外層的即可。

如果不嫌麻煩,非得一個個關(guān)閉,那么需要先關(guān)閉最里層,從里往外一層層進行關(guān)閉。

為什么不能從外層往里層逐步關(guān)閉?原因上面講裝飾流已經(jīng)講的很清楚了,關(guān)閉外層時,內(nèi)層的流其實已經(jīng)同時關(guān)閉了,你再去關(guān)內(nèi)層的流,就會報錯

至于網(wǎng)上說的先聲明先關(guān)閉,就是這個道理,先聲明的是內(nèi)層,最先申明的是最內(nèi)層,最后聲明的是最外層。

分割線-----------------------------

其實jdk8版的順序隨便打亂關(guān)閉都不會報錯,因為最里面的有判斷,如果流已經(jīng)關(guān)閉直接return)。

可以看FileInputStream源碼:

public void close() throws IOException {
        synchronized (closeLock) {
            if (closed) {
                return;
            }
            closed = true;
        }
        if (channel != null) {
           channel.close();
        }
 
        fd.closeAll(new Closeable() {
            public void close() throws IOException {
               close0();
           }
        });
    }

其他jdk版本,博主時間有限沒有測試,各位還是遵循老辦法(分割線前面的)關(guān)閉吧。

8.深究為什么一定要關(guān)閉流的原因

一個流綁定了一個文件句柄(或網(wǎng)絡端口),如果流不關(guān)閉,該文件(或端口)將始終處于被鎖定(不能讀取、寫入、刪除和重命名)狀態(tài),占用大量系統(tǒng)資源卻沒有釋放。

9.推薦使用NIO的Files工具類替換FileInputStream和FileOutputStream

public static?List<String> readAllLines(Path path, Charset cs)//以字符流方式讀取所有行

public static Path write(Path path, Iterable<? extends CharSequence> lines,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?Charset cs, OpenOption... options)//以字符流方式寫入指定行

public static?byte[] readAllBytes(Path path)//以字節(jié)流方式讀取所有字節(jié)

public static Path write(Path path, byte[] bytes, OpenOption... options)//以字節(jié)流方式寫入指定字節(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

怀仁县| 全州县| 克东县| 石门县| 仪征市| 明水县| 青岛市| 商城县| 奈曼旗| 濉溪县| 阿勒泰市| 和平县| 和顺县| 文水县| 墨玉县| 永吉县| 霍城县| 阳原县| 赤峰市| 那曲县| 谷城县| 揭东县| 宝兴县| 灵山县| 鄂伦春自治旗| 黎城县| 江津市| 吉林市| 西畴县| 宜兴市| 望城县| 同德县| 如东县| 公安县| 腾冲县| 宝应县| 宁都县| 虞城县| 镇沅| 营口市| 本溪市|