iOS路徑沙盒文件管理,看到博主總結的很好,轉載過來,原文:http://www.aichengxu.com/view/35264php
1、iOS中的沙盒機制
sql
iOS應用程序只能對本身建立的文件系統讀取文件,這個獨立、封閉、安全的空間,叫作沙盒。它通常存放着程序包文件(可執行文件)、圖片、音頻、視頻、plist文件、sqlite數據庫以及其餘文件。數據庫
每一個應用程序都有本身的獨立的存儲空間(沙盒)安全
通常來講應用程序之間是不能夠互相訪問網絡
模擬器沙盒的位置app
/User/userName/Library/Application Support/iPhone Simulatorui
當咱們建立應用程序時,在每一個沙盒中含有三個文件,分別是Document、Library和temp。編碼
Document:通常須要持久的數據都放在此目錄中,能夠在當中添加子文件夾,iTunes備份和恢復的時候,會包括此目錄。url
Library:設置程序的默認設置和其餘狀態信息spa
temp:建立臨時文件的目錄,當iOS設備重啓時,文件會被自動清除
獲取沙盒目錄
獲取程序的根目錄(home)目錄
NSString *homePath = NSHomeDirectory()
獲取Document目錄
NSArray *paths = NSSearchPathDorDirectoriesInDomains(NSDocumentDicrectory,, NSUserDomainMark, YES); NSString *docPath = [paths lastObject];
獲取Library目錄
NSArray *paths = NSSearchPathForDirectoriseInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths lastObject];
獲取Library中的Cache
NSArray *paths = NSSearchPathForDirectoriseInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *docPath = [paths lastObject];
獲取temp路徑
NSString *temp = NSTemporaryDirectory( );
2、NSString類路徑的處理方法
文件路徑的處理
NSString *path=@"/Uesrs/apple/testfile.txt"
經常使用方法以下
得到組成此路徑的各個組成部分,結果:("/","User","apple","testfile.txt")
- (NSArray *)pathComponents;
提取路徑的最後一個組成部分,結果:testfile.txt
- (NSString *)lastPathComponent;
刪除路徑的最後一個組成部分,結果:/Users/apple
- (NSString *)stringByDeletingLastPathCpmponent;
將path添加到先郵路徑的末尾,結果:/Users/apple/testfile.txt/app.txt
- (NSString *)stringByAppendingPathConmponent:(NSString *)str;
去路徑最後部分的擴展名,結果:text
- (NSString *)pathExtension;
刪除路徑最後部分的擴展名,結果:/Users/apple/testfile
- (NSString *)stringByDeletingPathExtension;
路徑最後部分追加擴展名,結果:/User/apple/testfile.txt.jpg
- (NSString *)stringByAppendingPathExtension:(NSString *)str;
3、NSData
NSData的用法
1.NSString與NSData互相轉換
NSData-> NSString
NSString *aString = [[NSString alloc] initWithData:adataencoding:NSUTF8StringEncoding];
NSString->NSData
NSString *aString = @"1234abcd"; NSData *aData = [aString dataUsingEncoding: NSUTF8StringEncoding];
將data類型的數據,轉成UTF8的數據
+(NSString *)dataToUTF8String:(NSData *)data
{
NSString *buf = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
return [buf autorelease];
}
將string轉換爲指定編碼
+(NSString *)changeDataToEncodinString:(NSData *)data encodin:(NSStringEncoding )encodin{ NSString *buf = [[[NSString alloc] initWithData:data encoding:encodin] autorelease]; return buf; }
2. NSData 與 UIImage
NSData->UIImage
UIImage *aimage = [UIImage imageWithData: imageData];
//例:從本地文件沙盒中取圖片並轉換爲NSData
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *name = [NSString stringWithFormat:@"ceshi.png"];
NSString *finalPath = [path stringByAppendingPathComponent:name];
NSData *imageData = [NSData dataWithContentsOfFile: finalPath];
UIImage *aimage = [UIImage imageWithData: imageData];
3.NSData與NSArray NSDictionary
+(NSString *)getLocalFilePath:(NSString *) fileName { return [NSString stringWithFormat:@"%@/%@%@", NSHomeDirectory(),@「Documents」,fileName]; }
包括將NSData寫進Documents目錄
從Documents目錄讀取數據
在進行網絡數據通訊的時候,常常會遇到NSData類型的數據。在該數據是dictionary結構的狀況下,系統沒有提供現成的轉換成 NSDictionary的方法,爲此能夠經過Category對NSDictionary進行擴展,以支持從NSData到NSDictionary的 轉換。聲明和實現以下:
+ (NSDictionary *)dictionaryWithContentsOfData:(NSData *)data { CFPropertyListRef list = CFPropertyListCreateFromXMLData(kCFAllocatorDefault, (CFDataRef)data, kCFPropertyListImmutable, NULL); if(list == nil) return nil; if ([(id)list isKindOfClass:[NSDictionary class]]) { return [(NSDictionary *)list autorelease]; } else { CFRelease(list); return nil; } }
4、文件管理經常使用方法
NSFileManager
建立一個文件並寫入數據
- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;
從一個文件中讀取數據
- (NSData *)contentsAtPath:(NSString *)path;
scrPath路徑上的文件移動到dstPath路徑上,注意這裏的路徑是文件路徑而不是目錄
- (BOOL)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath error:(NSError **) error;
scrPath路徑上的文件複製到dstPath路徑上
- (BOOL)copyItemAtPath:(NSString *)scrPath toPath:(NSString *)dstPath error:(NSError **) error;
比較兩個文件的內容是否同樣
- (BOOL)contentsEqualAtPath:(NSString *)path1 andPath:(NSString *)path2;
文件是否存在
- (BOOL)fileExistsAtPath:(NSString *)path;
移除文件
- (BOOL)removeItemAtPath:(NSString *)path error:(NSError **) error;
建立文件管理
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *path = [NSHomeDirectory( ) stringByAppendingPathComponent:@"holyBible.txt"];
將字符串轉成NSData類型
NSString *text = @"abcdefg"; NSData *data = [text dataUsingEncoding: NSUTF8StringEncoding];
寫入文件
BOOL success = [fileManager createFileAtPath:path contents:data attributes:nil];
建立文件夾
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSString *contect = @"abcdefg";
BOOL success = [fm createFileAtPath:filePath contents:[content dataUsingEncoding: NSUTF8StringEncoding] attributes:nil];
NSFileManager-讀取內容
NSData *fileData = [fileManager contentsAtPath:filePath]; NSString *content = [[NSString alloc] initWithData:fileData dataUsingEncoding: NSUTF8StringEncoding];
NSData-讀取內容
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"];
NSData *data = [NSData dataWithContentOfFile:filePath];
NSString-讀取內容
NSString *filePath = [path stringByAppendingPathComponent:@"holyBible.txt"]; NSString *content = [[NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
移動、複製文件
移動文件(重命名)
NSString *toPath = [NSHomeDirectory( ) stringByAppendingPathComponent:@"hellogod/New Testament.txt"];
[fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
NSError *error;
BOOL isSuccess = [fm moveItemAtPath:filePath toPath:toPath error:&error];
複製文件(重命名)
NSString *copyPath = [NSHomeDirectory( ) stringByAppendingPathComponent:@"備份/Old Testament.txt"];
[fm createDirectoryAtPath:[toPath stringByDeletingLastPathComponent] withIntermediateDirectories:YES attributes:nil error:nil];
BOOL success = [fm copyItemAtPath:toPath toPath:toPath error:nil];
刪除文件、獲取文件大小
判斷文件是否存在和刪除文件
if([fm fileExistsAtPath]) { if ([fm removeItemAtPath:copyPath]) { NSLog(@"remove success"); } }
獲取文件大小
NSFileManager *fileManager = [NSFileManager defaultManager];
得到文件的屬性字典
NSDictionary *attrDic = [fileManager attributesOfItemAtpath:sourcePath error:nil]; NSNumber *fileSize = [attrDic objectForKey:NSFileSize];
獲取目錄文件信息
NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *enuPath = [NSHomeDirectoty( ) stringByAppendingPathComponent:@"Test"]; NSDictionaryEnumerator *dirEnum = [fileManager enumeratorAtPath:enuPath]; NSString *path = nil; while ((path = [dirEnum nextObject]} != nil){ NSLog(@"%@",path); }