OC 檔案基本操作

2021-09-08 17:42:26 字數 4602 閱讀 4327

1//2

oc_134//

5//created by ma c on 15/12/17.6//

7//89

#import

10#define filepath @"/users/mac/desktop/"

11#define filepath1 @"/users/mac/desktop/bowen.txt"

1213

14int main(int argc, const

char *ar**)

53else

54 nslog(@"

檔案建立失敗");

5556

57//

建立資料夾

58 nsfilemanager *fm1 =[nsfilemanager defaultmanager];

5960 nsstring *directoryname = @"

bowen";

6263

64 bool flag1 =[fm1 createdirectoryatpath:directorypath withintermediatedirectories:yes attributes:nil error:nil];

6566

if(flag1)

69else

70 nslog(@"

資料夾建立失敗");

7172

//讀取檔案

73 nsfilemanager *fm2 =[nsfilemanager defaultmanager];

7475 nsstring *filename1 = @"

bowen.txt";

7778 nsdata *filedata1 =[fm2 contentsatpath:filepath1];

79 nsstring *filecontents =[[nsstring alloc] initwithdata:filedata1 encoding:nsutf8stringencoding];

80 nslog(@"%@"

,filecontents);

8182

83//

移動檔案

84 nsfilemanager *fm3 =[nsfilemanager defaultmanager];

8586 nsstring *oldname = @"

bowen.txt";

88 nsstring *newname = @"

bowen/newbowen.txt";

9091 bool flag2 =[fm3 moveitematpath:oldpath topath:newpath error:nil];

9293

if(flag2)

96else

97 nslog(@"

移動失敗");

9899

100101

//刪除檔案

102 nsfilemanager *fm5 =[nsfilemanager defaultmanager];

103104 nsstring *filename2 = @"

bowen.txt";

106107 bool flag4 =[fm5 removeitematpath:filepath2 error:nil];

108if

(flag4) else

111 nslog(@"

檔案刪除失敗");

112113

114//

拷貝檔案

115 nsfilemanager *fm4 =[nsfilemanager defaultmanager];

116117 nsstring *fromname = @"

bowen/newbowen.txt";

119 nsstring *atname = @"

bowen.txt";

121122 bool flag3 =[fm4 copyitematpath:frompath topath:atpath error:nil];

123124

if(flag3) else

127 nslog(@"

檔案拷貝失敗");

128129

130//

雜項操作

131 nsfilemanager *fm6 =[nsfilemanager defaultmanager];

132 nsstring *filename3 = @"

bowen.txt";

134135 bool flag5 =[fm6 fileexistsatpath:filepath3];

136137

if(flag5)

143else

144 nslog(@"

檔案不存在");

145146

//檔案遍歷(淺遍歷,深遍歷)

147148 nsfilemanager *fm7 =[nsfilemanager defaultmanager];

149150 nsstring *directoryname2 = @"

bowen";

152//

淺遍歷153 nsarray *filearr =[fm7 contentsofdirectoryatpath:directorypath2 error:nil];

154 nslog(@"%@"

,filearr);

155//

深遍歷156 nsarray *allfilearr =[fm7 subpathsofdirectoryatpath:directorypath2 error:nil];

157 nslog(@"%@"

,allfilearr);

158159

160161

162//

-----------nsfilehandle 檔案控制代碼類(操作檔案內容)------------

//163

//檔案讀取

164 nsfilehandle *fh =[nsfilehandle filehandleforreadingatpath:filepath1];

165166

167 nsdata *filedata2 = [fh readdataoflength:4

];168 nsstring *datastr =[[nsstring alloc] initwithdata:filedata2 encoding:nsutf8stringencoding];

169 nslog(@"%@"

,datastr);

170171

172 nsdata *filedata3 = [fh readdataoflength:4

];173 nsstring *datastr1 =[[nsstring alloc] initwithdata:filedata3 encoding:nsutf8stringencoding];

174 nslog(@"%@"

,datastr1);

175176 [fh seektofileoffset:0];//

移動游標位置

177178 nsdata *filedata4 =[fh readdatatoendoffile];

179 nsstring *datastr2 =[[nsstring alloc] initwithdata:filedata4 encoding:nsutf8stringencoding];

180 nslog(@"%@"

,datastr2);

181182

//檔案寫入

183 nsfilehandle *fh1 =[nsfilehandle filehandleforwritingatpath:filepath1];

184185 nsstring *contents1 = @"

hello horld";

186 nsdata *contents1data =[contents1 datausingencoding:nsutf8stringencoding];

187188

//[fh1 truncatefileatoffset:0];

//只能在寫入的時候用-移動游標位置

189[fh1 writedata:contents1data];

190191

}192

return0;

193194 }

oc檔案操作

documents 蘋果建議將程式建立產生的檔案以及應用瀏覽產生的檔案資料儲存在該目錄下,itunes備份和恢復的時候會包括此目錄 library 儲存程式的預設設定或其它狀態資訊 library caches 存放快取檔案,儲存應用的持久化資料,用於應用公升級或者應用關閉後的資料儲存,不會被itu...

OC學習 OC中對檔案的操作

下面是一些簡單的關於oc中對檔案的基本操作的案例 應用程式檔案的移動 nsfilemanager new moveitematpath nsstring 路徑格式 user nsstring型別 topath nsstring error nserror autoreleasing 應用程式檔案的複...

OC 檔案系統操作

1.檔案的路徑查詢 nslog nstemporarydirectory nstemporarydirectory 系統中用來建立臨時檔案的目錄路徑名 nslog nshomedirectory nshomedirectory 返回當前使用者的主目錄 nslog nshomedirectoryfor...