iOS 的目錄操作基礎

2021-05-25 16:10:36 字數 1680 閱讀 4256

1、檔案管理類:nsfilemanager

常用介面:

1.1 fileexistsatpath 判斷檔案是否存在

1.2 fileattributespath 判斷資料夾屬性,可讀/可寫

1.3 copypath 從乙個目錄拷貝乙個檔案到另外乙個目錄

1.4 movepath 從乙個目錄轉到檔案到另外乙個目錄

1.5 removefileatpath 刪除指定目錄中的檔案

2、ios可操作目錄有兩種:documents下與caches下, 如圖:

1、建立目錄createdirectoryatpath:withintermediatedirectories:attributes:error:

nsfilemanager * filemanager = nil;

nsarray *paths = nil;

nsstring *documentsdirectory = nil;

nsstring * folername = @"photos";

nsstring * filename = @"myphoto.png";

nsstring  * filepath = nil;

uiimage *photoimage = nil;

nsdata * imagedata = nil;

//documents:

paths = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);

//caches:

paths = nssearchpathfordirectoriesindomains(nscachesdirectory, nsuserdomainmask, yes);

filemanager = [[nsfilemanager alloc]init];

[filemanager createdirectoryatpath:documentsdirectory withintermediatedirectories:yes attributes:nil error:error];

2、建立目錄並在目錄中儲存物件createfileatpath: contents: attributes

if((![filemanager fileexistsatpath: fullpathtofile])) 

photoimage = [[uiimage alloc] imagenamed:@"photo.png"];

imagedata = uiimagepngrepresentation(photoimage);

[filemanager createfileatpath:filepath contents: imagedata attributes:nil];

[ photoimage release];

[filemanager release];

3、刪除目錄中指定檔案

[filemanager removeitematpath: filepath error:null];

iOS 檔案目錄操作

ios可操作目錄有兩種 documents下與caches下,如圖 1 建立目錄 createdirectoryatpath withintermediatedirectories attributes error nsfilemanager filemanager nil nsarray path...

Python基礎 目錄操作

os.mkdir 建立目錄 shutil.copyfile oldfile newfile 複製檔案 兩個引數必須是檔案 shutil.copy oldfile newfile oldfile只能是資料夾,newfile可以是檔案也可以是目標目錄 shutil.copytree oldfile ne...

iOS 操作檔案目錄的方法

使用目錄的常用方法 獲取當前目錄 nsstring currentdirectorypath 更改當前目錄 bool changecurrentdirectorypath nsstring path 複製目錄或檔案 bool copyitematpath nsstring srcpath topat...