標籤:
1.檔案儲存體
ios沙箱機制:應用程式只能訪問該應用程式在檔案系統中建立的目錄,
Documents檔案夾:文檔檔案夾,存放持久化的資料,
library檔案夾:caches:存放快取檔案,重啟或退出程式時,資料不會丟失
Perferences:喜好設定,存放使用者佈建資訊
tmp檔案夾:存放臨時的快取資料,在重啟或退出程式時清空;
基本檔案操作:
- (void)viewDidLoad
{
[super viewDidLoad];
// 檔案管理者
// NSFileManager
//建立檔案管理對象
NSFileManager *fm = [NSFileManager defaultManager];
//擷取Documents檔案夾路徑
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [array objectAtIndex:0];
// 建立檔案夾
// 1.建立檔案夾的路徑
NSString *directoryPath = [documentPath stringByAppendingPathComponent:@"testFile"];//設定檔案夾路徑,stringByAppendingPathComponent方法會自動用"/"分隔形成完整路徑,
[fm createDirectoryAtPath:directoryPath withIntermediateDirectories:YES attributes:nil error:nil];
// 2.建立檔案
// 拼接檔案的路徑
NSString *filePath = [directoryPath stringByAppendingPathComponent:@"file"];
// NSData 用來封裝資料的
// 存的都是位元據
// 可以存各種資料 字串 音頻 映像 數組 字典
// 把字串轉換成data
NSString *string = @"你好";
// UTF8 編碼
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
// 建立檔案
[fm createFileAtPath:filePath contents:data attributes:nil];
// 判斷檔案是否存在
BOOL success = [fm fileExistsAtPath:filePath];
NSLog(@"是否存在檔案%d",success);
// 來擷取某個路徑下的所有檔案的路徑
NSArray *subpathArray = [fm subpathsAtPath:directoryPath];
for (NSString *path in subpathArray)
{
NSLog(@"---->%@",path);
}
NSString *path2 = [directoryPath stringByAppendingPathComponent:@"file副本"];
// 判斷內容是否相等
// contentsEqualAtPath
BOOL success1 = [fm contentsEqualAtPath:filePath andPath:path2];
NSLog(@"success1 == %d",success1);
// 移動某路徑下的元素到另外一個路徑
NSString *destinationPath = [NSHomeDirectory() stringByAppendingPathComponent:@"a.txt"];
// 把檔案移動到某檔案路徑(而不是指的目錄)
[fm moveItemAtPath:filePath toPath:destinationPath error:nil];
// 複製到某個檔案路徑下
[fm copyItemAtPath:destinationPath toPath:filePath error:nil];
// 移除某路徑下的檔案
[fm removeItemAtPath:filePath error:nil];
}
ios資料存放區