1,訪問沙盒路徑
//1,home主目錄裏面有,Documents,Library,tmp和一個應用程序
NSLog(@"Home:%@",NSHomeDirectory());
//2,Documents
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",documentsPath);
//3,Library
NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",libraryPath);
//4,tmp
NSLog(@"tmp:%@",NSTemporaryDirectory());
//5,caches
NSString *cachesPath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",cachesPath);
//6,user
NSString *user = NSUserName();
NSLog(@"user:%@",user);
//7,NSBundle:圖片路徑
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];
NSLog(@"bundle:%@",bundle);
2,簡單文件寫入
//1,獲取文件路徑
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",documentsPath);
//2,拼接文件路徑
NSString *file = [documentsPath stringByAppendingString:@"/myText.txt"];
//3,準備寫入的內容
NSString *content = @"Hello World!";
//4,寫入(下面yes是原子性,整個寫入,no有多少寫多少)
[content writeToFile:file atomically:YES encoding:NSUTF8StringEncoding error:nil];
//讀取
NSString *readString = [NSString stringWithContentsOfFile:file encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",readString);
3,
//NSArray
//1,獲取document路徑
//2,拼接文件路徑
NSString *arrayPath = [documentsPath stringByAppendingString:@"/array.plist"];
//3,準備內容
NSArray *array = @[@"123",@"456",@"789"];
//4,寫入
[array writeToFile:arrayPath atomically:YES];
//5,讀取
NSArray *readArray = [NSArray arrayWithContentsOfFile:arrayPath];
NSLog(@"%@",readArray);
//NSDictionary
//1,獲取文件路徑
//2,拼接文件路徑
NSString *dictPath = [documentsPath stringByAppendingString:@"/dict.plist"];
//3,準備寫入的內容
NSDictionary *dict = @{@"name": @"張強",@"age":@"21"};
//4,寫入內容
[dict writeToFile:dictPath atomically:YES];
//5,讀取
NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:dictPath];
NSLog(@"%@",readDict);