FileManager 的基本操作

????var domainListPath: String?

? ? var signaturePath: String?

? ? var publicKeyPath: String?

1吧碾、從documentDirectory獲取文件?

? ? ? ? let sandBoxPath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).map(\.path)

? ? ? ? domainListPath = URL(fileURLWithPath: sandBoxPath[0]).appendingPathComponent("1.list").path

? ? ? ? signaturePath = URL(fileURLWithPath: sandBoxPath[0]).appendingPathComponent("2").path

? ? ? ? publicKeyPath = URL(fileURLWithPath: sandBoxPath[0]).appendingPathComponent("3.key").path


? ? ? ? 2、如果沒有就從bundle把文件復(fù)制到documents里面

? ? ? ? letfileMgr =FileManager.default

? ? ? ? fileMgr.delegate=self

? ? ? ? if(!fileMgr.fileExists(atPath:domainListPath!) ||

? ? ? ? ? ? !fileMgr.fileExists(atPath:signaturePath!) ||

? ? ? ? ? ? !fileMgr.fileExists(atPath:publicKeyPath!)) {

? ? ? ? ? ? do{

? ? ? ? ? ? ? ? tryfileMgr.copyItem(

? ? ? ? ? ? ? ? ? ? atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/domainip.list").path,

? ? ? ? ? ? ? ? ? ? toPath:domainListPath!)

? ? ? ? ? ? ? ? tryfileMgr.copyItem(

? ? ? ? ? ? ? ? ? ? atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/signature").path,

? ? ? ? ? ? ? ? ? ? toPath:signaturePath!)

? ? ? ? ? ? ? ? tryfileMgr.copyItem(

? ? ? ? ? ? ? ? ? ? atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/public.key").path,

? ? ? ? ? ? ? ? ? ? toPath:publicKeyPath!)

? ? ? ? ? ? }catch{

? ? ? ? ? ? }

? ? ? ? }

//MARK? FileManagerDelegate methods

? ? funcfileManager(_fileManager:FileManager,shouldCopyItemAtPathsrcPath:String,toPathdstPath:String) ->Bool{

? ? ? ? try? fileManager.removeItem(atPath: srcPath)

? ? ? ? if!FileManager.default.fileExists(atPath: srcPathasString) {

? ? ? ? ? ? do{

? ? ? ? ? ? ? ? tryFileManager.default.copyItem(atPath: srcPathasString,toPath: dstPathasString)


? ? ? ? ? ? }catch? {

? ? ? ? ? ? ? ? Log.Debug("func fileManager(_ fileManager: FileManager, shouldCopyItemAtPath srcPath: String, toPath dstPath: String) -> Bool")

? ? ? ? ? ? }

? ? ? ? }

? ? ? ? return true

? ? }


? ? //mark DCheckerManager delegate methods

? ? func overwriteConfigurations() {

? ? ? ? letfileMgr =FileManager.default

? ? ? ? fileMgr.delegate=self


? ? ? ? do{

? ? ? ? ? ? tryfileMgr.copyItem(

? ? ? ? ? ? ? ? atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/domainip.list").path,

? ? ? ? ? ? ? ? toPath:domainListPath!)

? ? ? ? ? ? tryfileMgr.copyItem(

? ? ? ? ? ? ? ? atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/signature").path,

? ? ? ? ? ? ? ? toPath:signaturePath!)

? ? ? ? ? ? tryfileMgr.copyItem(

? ? ? ? ? ? ? ? atPath: URL(fileURLWithPath: Bundle.main.resourcePath ?? "").appendingPathComponent("/default_list/public.key").path,

? ? ? ? ? ? ? ? toPath:publicKeyPath!)

? ? ? ? }catch{

? ? ? ? }

? ? }

歡迎大家加入QQ群一起交流學(xué)習(xí):

464406500

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末檬输,一起剝皮案震驚了整個濱河市十酣,隨后出現(xiàn)的幾起案子涩拙,更是在濱河造成了極大的恐慌,老刑警劉巖耸采,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件兴泥,死亡現(xiàn)場離奇詭異,居然都是意外死亡虾宇,警方通過查閱死者的電腦和手機搓彻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人好唯,你說我怎么就攤上這事竭沫。” “怎么了骑篙?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵蜕提,是天一觀的道長。 經(jīng)常有香客問我靶端,道長谎势,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任杨名,我火速辦了婚禮脏榆,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘台谍。我一直安慰自己须喂,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布趁蕊。 她就那樣靜靜地躺著坞生,像睡著了一般。 火紅的嫁衣襯著肌膚如雪掷伙。 梳的紋絲不亂的頭發(fā)上是己,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天,我揣著相機與錄音任柜,去河邊找鬼卒废。 笑死,一個胖子當(dāng)著我的面吹牛宙地,可吹牛的內(nèi)容都是我干的摔认。 我是一名探鬼主播,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼宅粥,長吁一口氣:“原來是場噩夢啊……” “哼参袱!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起粹胯,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤蓖柔,失蹤者是張志新(化名)和其女友劉穎辰企,沒想到半個月后风纠,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡牢贸,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年竹观,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡臭增,死狀恐怖懂酱,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情誊抛,我是刑警寧澤列牺,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站拗窃,受9級特大地震影響瞎领,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜随夸,卻給世界環(huán)境...
    茶點故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一九默、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧宾毒,春花似錦驼修、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至癌瘾,卻和暖如春觅丰,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背妨退。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工妇萄, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人咬荷。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓冠句,卻偏偏與公主長得像,于是被迫代替她去往敵國和親幸乒。 傳聞我的和親對象是個殘疾皇子懦底,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容