iOS 操作檔案目錄的方法

2021-06-18 08:08:59 字數 3659 閱讀 7714

使用目錄的常用方法:

//獲取當前目錄

- (nsstring *)currentdirectorypath

//更改當前目錄

- (bool)changecurrentdirectorypath:(nsstring *)path

//複製目錄或檔案

- (bool)copyitematpath:(nsstring *)srcpath topath:(nsstring *)dstpath error:(nserror **)error

//建立乙個新的目錄

- (bool)createdirectoryatpath:(nsstring *)path withintermediatedirectories:(bool)createintermediates attributes:(nsdictionary *)attributes error:(nserror **)error

//測試檔案是不是目錄

- (bool)fileexistsatpath:(nsstring *)path isdirectory:(bool *)isdirectory

//列出目錄下的內容(不會遞迴)

- (nsarray *)contentsofdirectoryatpath:(nsstring *)path error:(nserror **)error

//列舉目錄內容(會遞迴)

- (nsdirectoryenumerator *)enumeratoratpath:(nsstring *)path

//刪除乙個檔案,資料夾,鏈結

- (bool)removeitematpath:(nsstring *)path error:(nserror **)error

//移動(重新命名)目錄到乙個指定的路徑

- (bool)moveitematpath:(nsstring *)srcpath topath:(nsstring *)dstpath error:(nserror **)error

//操作目錄

nsfilemanager *fm = [[nsfilemanager alloc]init];

nsstring *dirname = @"testdir";

nsstring *path = [fm currentdirectorypath]; //獲取當前目錄路徑.

//新建目錄

if ([fm createdirectoryatpath:dirname withintermediatedirectories:yes attributes:nil error:null] == no)

//重新命名目錄

if ([fm moveitematpath:dirname topath:@"newdir" error:null] == no)

//修改路徑到新目錄

if ([fm changecurrentdirectorypath:@"newdir"] == no)

//返回當前路徑

path = [fm currentdirectorypath];

nslog(@"current directory path is %@",path);

//刪除目錄

if ([fm removeitematpath:path error:null] == no)

//列舉目錄中的內容

nsarray *dirarray;

nsfilemanager *fm = [[nsfilemanager alloc]init];

nsstring *dirpath = [fm currentdirectorypath]; //當前目錄

nsdirectoryenumerator *direnum = [fm enumeratoratpath:dirpath]; //開始列舉過程,將其存入direnum中.

//向direnum傳送nextobject訊息,返回下乙個檔案路徑,當沒有可供列舉時,返回nil.

//enumeratoratpath:方法會遞迴列印.

nsstring *file;

while ((file = [direnum nextobject]))

}dirarray = [fm contentsofdirectoryatpath:[fm currentdirectorypath] error:null];

nslog(@"內容為:"); //使用contentsofdirectoryatpath:方法列舉當前路徑中的檔案並存入陣列dirarray.

for (nsstring *path in dirarray)

nsfilemanager *fm = [[nsfilemanager alloc]init];   

nsstring *fname = @"path.m";

nsstring *upath = @"~s0s0/123/***/../321/./path.m";

nsstring *path, *tempdir, *extension, *homedir, *fullpath;

nsarray *components;

fm = [[nsfilemanager alloc]init];

//nstemporarydirectory()函式返回可以用來建立臨時檔案的目錄路徑名,如果要建立檔案,完成任務後要刪除;確保檔名是唯一的.

tempdir = nstemporarydirectory();

nslog(@"臨時資料夾路徑為:%@",tempdir);

path = [fm currentdirectorypath]; //返回當前目錄路徑

//lastpathcomponent方法用與提取路徑中最後乙個目錄名.

nslog(@"父目錄名: %@",[path lastpathcomponent]);

nslog(@"完整路徑為:%@ to %@",fname,fullpath);

//pathextension方法返回乙個完整路徑中的副檔名,如果沒有副檔名,就返回空字元.

extension = [fullpath pathextension];

nslog(@"extension for %@ is %@ ",fullpath, extension);

//nshomedirectory()函式返回當前使用者的主目錄

//nshomedirectoryforuser(username)函式可以提供使用者名稱做引數,並返回主目錄名

homedir = nshomedirectory();

nslog(@"主目錄為 : %@",homedir);

//pathcomponents方法返回乙個陣列,陣列中包含乙個路徑中的每個組成部分.

components = [homedir pathcomponents];

for (path in components)

//stringbystandardizingpath方法將原路徑中的代字元轉化為完整路徑.

//如果是路徑名字中出現代字元,可以使用stringbyexpandingtildeinpath方法.

nslog(@"%@ -> %@",upath, [upath stringbystandardizingpath]);

iOS 操作檔案目錄的方法

使用目錄的常用方法 cpp view plain copy 獲取當前目錄 nsstring currentdirectorypath 更改當前目錄 bool changecurrentdirectorypath nsstring path 複製目錄或檔案 bool copyitematpath ns...

Linux 操作檔案目錄

命令 說明語法 引數引數說明 ls顯示檔案和目錄列表 ls alrtafr name.l列出檔案的詳細資訊 a列出當前目錄所有檔案,包含隱藏檔案 mkdir 建立目錄 mkdir p dirname p父目錄不存在情況下先生成父目錄 cd切換目錄 cd dirname touch 生成乙個空檔案 e...

26 操作檔案目錄

操作檔案和目錄的函式一部分在os模組中,一部分在os.path模組中。檢視 建立和刪除目錄 檢視當前目錄的絕對路徑 os.path.abspath users michael 在某個目錄下建立乙個新目錄,首先把新目錄的完整路徑表示出來 os.path.join users michael testd...