日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

Java如何实现文件拷贝操作和如何正确关闭资源

發(fā)布時間:2024/9/20 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java如何实现文件拷贝操作和如何正确关闭资源 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

使用字節(jié)流完成文件的拷貝:

使用字節(jié)輸入流(FileInputStream)將源文件中的數(shù)據(jù)讀進(jìn)來,同時使用字節(jié)輸出流(FileOutputStream)將讀進(jìn)來的數(shù)據(jù)寫到目標(biāo)文件中,即一邊讀一邊寫,完成文件的拷貝。

//使用字節(jié)流完成文件的拷貝操作
public class FileStremCopyDemo {
?? ?public static void main(String[] args) throws IOException {
?? ??? ?//創(chuàng)建目標(biāo)與源對象
?? ??? ?File srcFile = new File("file/src.txt");//源對象
?? ??? ?File desFile = new File("file/des.txt");//目標(biāo)文件
?? ??? ?
?? ??? ?//創(chuàng)建輸入輸出流
?? ??? ?InputStream in = new FileInputStream(srcFile);
?? ??? ?OutputStream out = new FileOutputStream(desFile);
?? ??? ?
?? ??? ?//IO 操作
?? ??? ?byte[] buffer = new byte[1024];//創(chuàng)建容量為1024的緩沖區(qū)(存儲已經(jīng)讀取的字節(jié)數(shù)據(jù))
?? ??? ?int len = -1;//表示已經(jīng)讀取了多少個字節(jié)數(shù)據(jù),若果等于-1,表示已經(jīng)讀到最后
?? ??? ?while((len = in.read(buffer)) != -1){
?? ??? ??? ?//數(shù)據(jù)在buffer數(shù)組中
?? ??? ??? ?out.write(buffer, 0, len);
?? ??? ?}
?? ??? ?//關(guān)閉資源
?? ??? ?in.close();
?? ??? ?out.close();
?? ?}
?
}

上面程序?qū)崿F(xiàn)文件的拷貝中,是直接將異常拋出去,一般這種情況是要處理異常的,按正常的try catch處理異常,我們會發(fā)現(xiàn)關(guān)閉資源的代碼會很繁瑣,如下:
?? ?//繁瑣的資源關(guān)閉方式
?? ?private static void test1() {
?? ??? ?File srcFile = new File("file/src.txt");
?? ??? ?File desFile = new File("file/des.txt");
?? ??? ?
?? ??? ?InputStream in = null;
?? ??? ?OutputStream ?out = null;
?? ??? ?try {
?? ??? ??? ?in = new FileInputStream(srcFile);
?? ??? ??? ?out = new FileOutputStream(desFile);
?
?? ??? ??? ?byte[] buffer = new byte[1024];
?? ??? ??? ?int len = -1;
?
?? ??? ??? ?while ((len = in.read(buffer)) != -1) {
?? ??? ??? ??? ?out.write(buffer);
?? ??? ??? ?}
?
?? ??? ?} catch (FileNotFoundException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?} catch (IOException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?} finally {
?? ??? ??? ?try {
?? ??? ??? ??? ?if (in != null) {
?? ??? ??? ??? ??? ?in.close();
?? ??? ??? ??? ?}
?? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ?}
?? ??? ??? ?
?? ??? ??? ?if (out != null) {
?? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ?out.close();
?? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ?}
?? ??? ??? ?}
?? ??? ?}
?? ?}

從Java7開始,Java新添了一個特性,在try后面加上一個圓括號,將需要關(guān)閉的資源放到里面定義,程序執(zhí)行完畢會自動幫我們關(guān)閉圓括號里面的資源,如下:
?? ?//Java7自動關(guān)閉資源
?? ?private static void test2() {
?? ??? ?File srcFile = new File("file/src.txt");
?? ??? ?File desFile = new File("file/des.txt");
?
?? ??? ?try (
?? ??? ??? ??? ?InputStream in = new FileInputStream(srcFile);
?? ??? ??? ??? ?OutputStream out = new FileOutputStream(desFile);
?? ??? ??? ??? ?) {
?
?? ??? ??? ?byte[] buffer = new byte[1024];
?? ??? ??? ?int len = -1;
?
?? ??? ??? ?while ((len = in.read(buffer)) != -1) {
?? ??? ??? ??? ?out.write(buffer);
?? ??? ??? ?}
?
?? ??? ?}catch (IOException e) {
?? ??? ??? ?e.printStackTrace();
?? ??? ?}
?
?? ?}

————————————————
版權(quán)聲明:本文為CSDN博主「caidie_huang」的原創(chuàng)文章,遵循CC 4.0 BY-SA版權(quán)協(xié)議,轉(zhuǎn)載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/caidie_huang/article/details/52738225

總結(jié)

以上是生活随笔為你收集整理的Java如何实现文件拷贝操作和如何正确关闭资源的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。