FileManager 檔案管理器

2021-08-29 15:46:13 字數 4593 閱讀 3789

1.獲得沙盒根目錄

nsstring *homepath = nshomedirectory();

2.獲取documents目錄路徑

方法一:

方法二:

nsarray *path = nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes);

nsstring *documentsdirectory = [path objectatindex:0];

3.獲取library/preferences目錄路徑

nsarray *paths = nssearchpathfordirectoriesindomains(nslibrarydirectory, nsuserdomainmask, yes);

nsstring *cachedirectory = [paths objectanindex:0];

4.獲取library/caches路徑

nsarray *paths = nssearchpathfordirectoriesdomains(nscachedirectory, nsuserdomainmask, yes);

nsstring *cachedirectory = [paths objectatindex:0];

5.獲取tmp路徑

nsstring *tmpdirectory = nstemporarydirectory();

4.nsfilemanager:用於執行一般的檔案系統操作

讀取檔案、向檔案中寫入資料、檔案刪除、修改、移動、比較兩個檔案的內容、測試檔案的存在性、讀取/更改檔案的屬性

1.初始化檔案管理器

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

2.如果要監聽檔案處理的結果需要設定**

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

filemanager.delegate = self;

3.判斷乙個給定的路徑是否為資料夾

bool createpathok = yes;

[[nsfilemanager defaultmanager] fileexistsatpath:filepath isdirectory: &createpathok];

4.指向沙盒中的documents資料夾

5.在documents資料夾下建立乙個名為「myfolder」資料夾

注意資料夾是沒有字尾的,檔案是有字尾的

/user/sunjian/documents/myfolder

/user/sunjian/documents/myfolder/text.txt

6.建立乙個檔案

7.向檔案中寫入資料

nsstirng *str = @"用來測試";

[str writetofile:filepath atomically:yes encoding:nsutf8stringencoding error:&error];

8.獲資料夾下的檔案目錄

nsarray *content = [filemanager contentsofdirectoryatpath:documentdiretory error: &error];

9.移除檔案

[filemanager removeitematpath:filepath error:&error];

10.路徑下是否存在檔案

bool i***ist = [filemanager fileexistsaapath:filepath];

11.從檔案中獲取資料

nsdata *mydata = [filemanager contentsatpath:path];

12.建立乙個檔案並寫入資料

[filemanager createfileatpath:path contents:mydata attributes:dic];

13.string與data互轉

nsstring *str = @"sunjian";

nsdata *data = [str datausingencoding:nsutf8stringencoding];

nsstring *str = [[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding];

//檔案細粒度操作

nsfilemanager *manager = [nsfilemanageer defaultmanager];

nsstring *filepath = @"/users/sunjian/desktop/document/test.txt";//最簡單的事就是程式設計

//以唯讀的方式開啟檔案

nsfilehandle *filehandle = [nsfilehandle filehandleforreadingatpath:filepath];

nsdata *data = [filehandle readdatatoendoffile];//完整的讀取檔案

//以可寫的方式開啟檔案作業系統,這才是重要的,要知道其原理肯定能實現

[nsfilehandle filehandlforwritingatpath:newpath];

//寫入檔案

[filehandle writedata:data];

//關閉檔案

[filehandle closefile];

//定位到指定的位置,預設在檔案開頭

[filehandle seektofileoffset:12];

[filehandle readdatatoendoffile];

component:所包含的內容

將ios和linux聯絡起來

1.當前檔案所在的目錄(pwd)

nsstring *currentpath = [manager currentdirectorypath];

2.建立目錄(mkdir)

bool result = [manager createdirectorhatpath:mypath withintermediatedirectories:yes attributes:nil error:nil];

3.目錄重新命名(mv srcname to dirname)

bool result = [manager moveitematpath:oldpath topath:newpath error:&error];

4.改變當前目錄

[manager changecurrentdirectorypath:newpath];

6.遍歷整個目錄(ls)

方法1:

nsdirectoryenumerator * diretocyenumerator = [namager enumeratoratpath:newpath];

while (path = [directoryenumerator nextobject])

方法2:

nsarray *paths = [manager contentsofdirectoryatpath:newpath error:nil];

nsobject *p;

for(p in paths)

8.刪除目錄(rm)

bool result = [manager removeitematpath:newpath error:nil];

9.檔案操作, 判斷檔案是否存在(find、where)

[manager filtexistsatpath:filepath isdirectory:no];

10.檔案是否可讀(isreadable)

[manager isreadableatpath:filepath];

11.判斷兩檔案呢內容是否相等(equal)

[namager contentsequalatpath:filepath andpath:filepath2];

12.讀取檔案屬性(attributes)

nsdictionary *attributes;

if((attributes = [manager attributesofitemanpath:newpath error:nil]) == nilt)

13.檔案細粒度操作,以唯讀的方式開啟檔案

nsfilehandle *filehandle = [nsfilehandle filehandleforreadingatpath:filepath];

14.讀取檔案

nsdata *data  = [filehandle readdatatoendoffile];

15.以寫的方式開啟檔案

nsfilehandle *filehandle2 = [nsfilehandle filehandleforwritingatpath:newpath];

16.寫入檔案

[filehandel writedata:data];

17.關閉檔案

[filehandle closefile];

18.定位檔案

[filehandle seektofileoffset:12];

python檔案管理 python檔案管理

檔案io常見操作 open 開啟 read 讀取 write 寫入 close 關閉 readline 行讀取 readlines 多行讀取 seek 檔案指標操作 tell 指標位置 開啟操作 open file,mode r buffering 1,encoding none,erroes no...

檔案管理 檔案狀態

檔案狀態包括檔案的大小,檔案的所有者,檔案的修改日期。這些資訊和檔案的內容是分開儲存的。使用如下函式得到檔案的狀態。include int stat const char file name,struct stat buf 函式說明 stat 用來將引數file name所指的檔案狀態,複製到引數b...

檔案管理 使用者管理 磁碟管理

檔案管理 檔案管理是作業系統的五大職能之一,主要涉及檔案的邏輯組織和物理組織,目錄的結構和管理。所謂檔案管理,就是作業系統中實現檔案統一管理的一組軟體 被管理的檔案以及為實施檔案管理所需要的一些資料結構的總稱 是作業系統中負責訪問和管理檔案資訊的機構 從系統角度來看,檔案系統 是對檔案 儲存器的儲存...