這篇文章主要是針對沙盒存儲方式的文件操作,詳文如下:
對於一個運行在iPhone得app,它只能訪問自己根目錄下的一些文件(所謂sandbox - 沙盒).
一個app發布到iPhone上後,它的目錄結構如下:
1、其中得 app root 可以用 NSHomeDirectory() 訪問到;
2、Documents 目錄就是我們可以用來寫入並保存文件得地方,一般可通過下面的方式得到:
[cpp] view plaincopyprint?NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
3、tmp 目錄我們可以在裡面寫入一些程序運行時需要用得數據,裡面寫入得數據在程序退出後會沒有。可以通過
NSString *NSTemporaryDirectory(void); 方法得到;
4、文件一些主要操作可以通過NSFileManage 來操作,可以通過 [NSFileManger defaultManger] 得到它得實例。
相關得一些操作:
a.創建一個目錄或者文件:
比如要在Documents下面創建一個test目錄,
[cpp]
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@”%@”,documentsDirectory);
NSFileManager *fileManage = [NSFileManager defaultManager];
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];
BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@”%@”,documentsDirectory);
NSFileManager *fileManage = [NSFileManager defaultManager];
NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];
BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil];
比如要在Documents下面創建一個file.txt:
[html]
// 結果為:/Documents/file.txt
NSString *filePath= [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
// 結果為:/Documents/file.txt
NSString *filePath= [documentsDirectory stringByAppendingPathComponent:@"file.txt"];
b.取得一個目錄下得所有文件名:
[cpp]
//如上面的myDirectory)可用
NSArray *file = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];
或
NSArray *files = [fileManager subpathsAtPath: myDirectory ];
//如上面的myDirectory)可用
NSArray *file = [fileManager subpathsOfDirectoryAtPath: myDirectory error:nil];
或
NSArray *files = [fileManager subpathsAtPath: myDirectory ];
c.讀取某個文件:
[cpp]
NSData *data = [fileManger contentsAtPath:myFilePath];//myFilePath是包含完整路徑的文件名
或直接用NSData 的類方法:
NSData *data = [NSData dataWithContentOfPath:myFilePath];
NSData *data = [fileManger contentsAtPath:myFilePath];//myFilePath是包含完整路徑的文件名
或直接用NSData 的類方法:
NSData *data = [NSData dataWithContentOfPath:myFilePath]; d.保存某個文件:
[cpp] view plaincopyprint?//可以用 NSFileManager的下列方法:
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
或 NSData 的
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;
//可以用 NSFileManager的下列方法:
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
或 NSData 的
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToFile:(NSString *)path options:(NSUInteger)writeOptionsMask error:(NSError **)errorPtr;
e.刪除某個文件:
[cpp] view plaincopyprint?//可以用 NSFileManager的下列方法:
//Removes the file or directory at the specified path.
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
//Removes the file or directory at the specified URL.
- (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error
//可以用 NSFileManager的下列方法:
//Removes the file or directory at the specified path.
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **)error
//Removes the file or directory at the specified URL.
- (BOOL)removeItemAtURL:(NSURL *)URL error:(NSError **)error
f.移動某個文件或者重命名某文件
[cpp]
//想要重命名一個文件,我們需要把文件移到一個新的路徑下。下面的代碼創建了我們所期望的目標文件的路徑,然後請求移動文件以及在移動之後顯示文件目錄。
//通過移動該文件對文件重命名
NSString *filePath2= [documentsDirectory stringByAppendingPathComponent:@"file2.txt"];
//判斷是否移動
if ([fileManager moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)
NSLog(@"Unable to move file: %@", [error localizedDescription]);
//顯示文件目錄的內容
NSLog(@"Documentsdirectory: %@",[fileManager contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
//想要重命名一個文件,我們需要把文件移到一個新的路徑下。下面的代碼創建了我們所期望的目標文件的路徑,然後請求移動文件以及在移動之後顯示文件目錄。
//通過移動該文件對文件重命名
NSString *filePath2= [documentsDirectory stringByAppendingPathComponent:@"file2.txt"];
//判斷是否移動
if ([fileManager moveItemAtPath:filePath toPath:filePath2 error:&error] != YES)
NSLog(@"Unable to move file: %@", [error localizedDescription]);
//顯示文件目錄的內容
NSLog(@"Documentsdirectory: %@",[fileManager contentsOfDirectoryAtPath:documentsDirectoryerror:&error]);
iPhone官方SDK用於讀寫數據的方法
我們知道,出於安全考慮,iPhone的官方SDK並不能像toolchain一樣隨意寫文件。
注意:這兩個方法都是存儲在/Documents/裡面。
[cpp]
bool writeApplicationData(NSData *data, NSString *fileName)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
return NO;
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return ([data writeToFile:appFile atomically:YES]);
}
NSData *applicationDataFromFile(NSString *fileName)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
return myData;
}
bool writeApplicationData(NSData *data, NSString *fileName)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if (!documentsDirectory) {
NSLog(@"Documents directory not found!");
return NO;
}
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
return ([data writeToFile:appFile atomically:YES]);
}
NSData *applicationDataFromFile(NSString *fileName)
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];
NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];
return myData;
}