檔案的基本操作

2021-06-26 15:23:50 字數 3204 閱讀 4104

/*________________________1.建立檔案____________________________*/

/*//獲取本地沙盒路徑

nsstring *homepath = nshomedirectory();

//設定檔案路徑

// nsfilemanager

// nsfilemanager *filemanager = [[nsfilemanager alloc] init]; 錯誤

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

//建立需要顯示的內容

nsstring *string = @"啊汙染物";

//轉換成nsdata

nsdata *data = [string datausingencoding:nsutf8stringencoding];

//filepath:建立的檔案的所放置的位置,這個方法是用來建立檔案,不是資料夾

bool success = [filemanager createfileatpath:filepath contents:data attributes:nil];

if (success) else

*//*________________________2.建立資料夾____________________________*/

/*nserror *error = nil;

bool success2 = [filemanager createdirectoryatpath:fpath

withintermediatedirectories:yes

attributes:nil

error:&error];

if (success2) else

nsdata *data = [nsdata datawithcontentsoffile:filepath];

*//*________________________3.讀取檔案____________________________*/

/*//本地沙盒路徑

nsstring *homepath = nshomedirectory();

//設定檔案路徑

//方式一:

// nsdata *data = [nsdata datawithcontentsoffile:filepath];

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

// nslog(@"string:%@",string);

//方式二:

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

nsdata *data = [filemanager contentsatpath:filepath];

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

nslog(@"string:%@",string);

*//*________________________4.移動檔案(剪下)____________________________*/

/* //本地沙盒路徑

nsstring *homepath = nshomedirectory();

//檔案以前所在的路徑

//需要移動到的路徑

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

nserror *error = nil;

bool success = [filemanager moveitematpath:filepath topath:topath error:&error];

if (success) else

//注意:可以用這種方式修改檔案的名字

*/ /*________________________5.複製檔案____________________________*/

/*//本地沙盒路徑

nsstring *homepath = nshomedirectory();

//設定檔案所在路徑

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

nserror *errror = nil;

bool success = [filemanager copyitematpath:filepath topath:copypath error:&errror];

if (success) else

*//*________________________6.刪除檔案____________________________*/

/*//本地沙盒路徑

nsstring *homepath = nshomedirectory();

//設定檔案所在路徑

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

//判斷檔案是否存在於相應的路徑下

if ([filemanager fileexistsatpath:filepath])

}//清除快取

*//*_____________________6.檔案屬性_________________________*/

nsfilemanager *filemanager = [nsfilemanager defaultmanager];

nsdictionary *attributes = [filemanager attributesofitematpath:filepath error:nil];

// nslog(@"attributes:%@",attributes);

//檔案的大小,單位是位元組

nsnumber *filesize = [attributes objectforkey:nsfilesize];

long size = [filesize longvalue];

nslog(@"檔案大小:%ld位元組",size);

return yes;

}@end

檔案的基本操作

使用cfile類新型檔案操作 一 1.讀取檔案資訊 virtual bool open lpctstr lpszfilename.uint nopenflags,cfileexception perror null lpszfilename 要開啟的檔名,可以包含完整路徑,也可以是相對繁的檔名 no...

檔案的基本操作

檔案可以按找兩種方式處理,一種是按照字串進行處理,另一種是按照二進位制進行處理。常見的文字型別有txt,log,py等。常見的二進位制檔案型別有exe,jpg,mp4等。編碼使用的作業系統預設編碼 中文windows預設編碼是gbk 英文windows預設編碼是utf 8。語法 open 檔名,模式...

基本的檔案操作

檔案是作業系統為使用者或應用程式提供的乙個讀寫硬碟的虛擬單位。檔案的操作是基於檔案,即檔案的操作核心就是 讀和寫。也 就是只要我們想要操作檔案就是對作業系統發起請求,然後由作業系統將使用者或應用程式對檔案的讀寫操作轉換成集體的硬碟指令 比如控制碟片轉動,控制機械手臂移動,以此來讀取資料 記憶體無法永...