io流File專案實戰 目錄拷貝

2021-10-11 10:35:31 字數 1378 閱讀 8209

關於使用io流進行目錄的拷貝

原始檔》目標檔案》呼叫方法進行拷貝

首先在開始之前要有清晰的思路。在整個過程中怎麼使用遞迴是重點。而且對於檔案路徑、檔名、拷貝方法、檔案的讀寫熟練的掌握。

建立原始檔和目標檔案物件 構造並呼叫方法

//拷貝原始檔

file srcfile =

newfile

("d:\\d\\f");

//拷貝目標

file destfile =

newfile

("c:\\a\\");

//呼叫方法拷貝

copydis

(srcfile,destfile)

;

使用if語句判斷讀到的是否是乙個檔案

目標檔案目錄destfile.getabsolutepath()加上srcfile.getabsolutepath().substring(3)原始檔的目錄 為最終拷貝完檔案的目錄。然後採用陣列方式進行拷貝。

if

(srcfile.

isfile()

)//重新整理

out.

flush()

;

獲取原始檔下的子目錄同時在目標檔案中拷貝原始檔的目錄

file[

] files =srcfile.

listfiles()

;for

( file file : files)

/** * 拷貝目錄

* @param srcfile 拷貝源

* @param destfile 拷貝目標

*/private

static

void

copydis

(file srcfile,file destfile)

//重新整理

out.

flush()

;}catch

(filenotfoundexception e)

catch

(ioexception e)

finally

catch

(ioexception e)}if

(in != null)

catch

(ioexception e)}}

return;}

//獲取原始檔下面的子目錄

file[

] files =srcfile.

listfiles()

;for

( file file : files)

}//遞迴呼叫

copydis

(file,destfile);}

}}

IO流 前置內容 File

例如 file f new file e demo a.txt 或者e demo a.txt 裡面的有可能是目錄名,也有可能是檔名.注意a.txt寫了不一定代表存在這個檔案,只是描述了一句話而已.建立 public boolean createnewfile 需要拋異常,並且該路徑必須存在,只能單純...

判斷file空 IO流(二) File類

file類的建構函式 通過將給定路徑名字串轉換為抽象路徑名來建立乙個新 file 例項。引數 string pathname 字串的路徑名稱 路徑可以是以檔案結尾,也可以是以資料夾結尾 路徑可以是相對路徑,也可以是絕對路徑 路徑可以是存在,也可以是不存在 建立file物件,只是把字串路徑封裝為fil...

IO流輸入輸出 File

file類是io包中唯一代表磁碟檔案本身的物件。通過file來建立,刪除,重新命名檔案。常用方法 file file1 new file src a.txt 指定當前workspace src下的a.txt檔案 file file2 new file c users l xd desktop tem...