日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

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

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

使用字節流完成文件的拷貝:

使用字節輸入流(FileInputStream)將源文件中的數據讀進來,同時使用字節輸出流(FileOutputStream)將讀進來的數據寫到目標文件中,即一邊讀一邊寫,完成文件的拷貝。

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

上面程序實現文件的拷貝中,是直接將異常拋出去,一般這種情況是要處理異常的,按正常的try catch處理異常,我們會發現關閉資源的代碼會很繁瑣,如下:
?? ?//繁瑣的資源關閉方式
?? ?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后面加上一個圓括號,將需要關閉的資源放到里面定義,程序執行完畢會自動幫我們關閉圓括號里面的資源,如下:
?? ?//Java7自動關閉資源
?? ?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();
?? ??? ?}
?
?? ?}

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

總結

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

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。