NSFileManager
[self.fileManagerfileExistsAtPath:isDirectory:];
用於執行一般的文件系統操作 (reading and writing is done via NSData, et. al.).
主要功能包括:從一個文件中讀取數據;向一個文件中寫入數據;刪除文件;複製文件;移動文件;比較兩個文件的內容;測試文件的存在性;讀取/更改文件的屬性... ...
Just alloc/init an instance and start performing operations. Thread safe.
- 常見的NSFileManager處理文件的方法如下:
NSFileManager *fileManager = [[NSFileManager alloc]init]; //最好不要用defaultManager。
NSData *myData = [fileManager contentsAtPath:path]; // 從一個文件中讀取數據
[fileManager createFileAtPath:path contents:myData attributes:dict];//向一個文件中寫入數據,屬性字典允許你制定要創建
[fileManager removeItemAtPath:path error:err];
[fileManager moveItemAtPath:path toPath:path2 error:err];
[fileManager copyItemAtPath:path toPath:path2 error:err];
[fileManager contentsEqualAtPath:path andPath:path2];
[fileManager fileExistsAtPath:path]; ... ...
- 常見的NSFileManager處理目錄的方法如下:
[fileManager currentDirectoryPath];
[fileManager changeCurrentDirectoryPath:path];
[fileManager copyItemAtPath:path toPath:path2 error:err];
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:err];
[fileManager fileExistsAtPath:path isDirectory:YES];
[fileManager enumeratorAtPath:path]; //獲取目錄的內容列表。一次可以枚舉指定目錄中的每個文件。 ... ...Has a delegate with lots of “should” methods (to do an operation or proceed after an error).
And plenty more. Check out the documentation.
1、文件的創建
-(IBAction) CreateFile { //對於錯誤信息 NSError *error; // 創建文件管理器 NSFileManager *fileMgr = [NSFileManager defaultManager]; //指向文件目錄 NSString *documentsDirectory= [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
[[NSFileManager defaultManager] createDirectoryAtPath: [NSString stringWithFormat:@"%@/myFolder", NSHomeDirectory()] attributes:nil]; // File we want to create in the documents directory我們想要創建的文件將會出現在文件目錄中 // Result is: /Documents/file1.txt結果爲:/Documents/file1.txt NSString *filePath= [documentsDirectory stringByAppendingPathComponent:@"file2.txt"]; //需要寫入的字符串 NSString *str= @"iPhoneDeveloper Tips\nhttp://iPhoneDevelopTips,com"; //寫入文件 [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error]; //顯示文件目錄的內容
NSLog(@"Documentsdirectory: %@",[fileMgr contentsOfDirectoryAtPath:documentsDirectory
error:&error]); |
|
2、對文件重命名
對一個文件重命名 想要重命名一個文件,我們需要把文件移到一個新的路徑下。下面的代碼創建了我們所期望的目標文件的路徑,然後請求移動文件以及在移動之後顯示文件目錄。 //通過移動該文件對文件重命名 NSString *filePath2= [documentsDirectory stringByAppendingPathComponent:@"file2.txt"]; //判斷是否移動 if ([fileMgr moveItemAtPath:filePath toPath:filePath2 error:&error] != YES) NSLog(@"Unable to move file: %@", [error localizedDescription]); //顯示文件目錄的內容 NSLog(@"Documentsdirectory: %@", [fileMgr contentsOfDirectoryAtPath:documentsDirectoryerror:&error]); |
|
3、刪除一個文件
這些示例能教你的,僅僅只是文件處理上的一些皮毛。想要獲得更全面、詳細的講解,你就需要掌握NSFileManager文件的知識。 |
|
4、刪除目錄下所有文件
//獲取文件路徑 NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; NSString *path = [document stringByAppendingPathComponent:@"Attchments"];
[manager createDirectoryAtPath:path withIntermediateDirectories:NO attributes:nil error:nil];
} }
--清除附件 |
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager]fileExistsAtPath:filePath])
{
//do some thing
}
附:
-(NSString *)dataFilePath
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
return [documentDirectory stringByAppendingPathComponent:@"data.plist"];
}
============================================================================
NSFileHandle類允許更有效地使用文件。
可以實現如下功能:
1、打開一個文件,執行讀、寫或更新(讀寫)操作;
2、在文件中查找指定位置;
3、從文件中讀取特定數目的字節,或將特定數目的字節寫入文件中
另外,NSFileHandle類提供的方法也可以用於各種設備或套接字。一般而言,我們處理文件時都要經歷以下三個步驟:
1、打開文件,獲取一個NSFileHandle對象(以便在後面的I/O操作中引用該文件)。
2、對打開文件執行I/O操作。
3、關閉文件。
NSFileHandle *fileHandle = [[NSFileHandle alloc]init];
fileHandle = [NSFileHandle fileHandleForReadingAtPath:path]; //打開一個文件準備讀取
fileHandle = [NSFileHandle fileHandleForWritingAtPath:path];
fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:path];
fileData = [fileHandle availableData]; // 從設備或者通道返回可用的數據
fileData = [fileHandle readDataToEndOfFile];
[fileHandle writeData:fileData]; //將NSData數據寫入文件
[fileHandle closeFile]; //關閉文件 ... ...注:NSFileHandle類沒有提供創建文件的功能,所以必須使用NSFileManager來創建文件