//
// ViewController.m
// sandBox
//
// Created by zhaoguodong on 16/6/24.
// Copyright ? 2016年 zhaoguodong. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSString *sandBoxpath = NSHomeDirectory();
# #注意每個(gè)文件夾的名字,大小寫不能錯(cuò)
/**
* 每個(gè)沙盒路徑下都有三個(gè)文件夾
*Documents 是最常用的,可以儲(chǔ)存一些不太大的數(shù)據(jù),plist、文本、splite。它會(huì)和itunes同步
!
*Library
caches:儲(chǔ)存大容量的數(shù)據(jù),caches路徑的文件不會(huì)和itunes同步
preference:儲(chǔ)存用戶設(shè)置。(偏好設(shè)置)
tmp 臨時(shí)文件夾,不進(jìn)行同步,當(dāng)設(shè)備重啟,就會(huì)清空。
*/
// 拼接路徑獲取documents路徑
NSString *path = [sandBoxpath stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@",path);
/**
* 直接獲取路徑
*
* @第一個(gè)參數(shù):尋找的哪一個(gè)文件夾
* @第二個(gè)參數(shù):用戶沙盒主目錄
* @第三個(gè)參數(shù):如果為 NO 則不能完成讀寫操作
*/
NSString *path1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSLog(@"%@",path1);
//獲取preference 一般通過NSuserDefaults
[[NSUserDefaults standardUserDefaults] setObject:@"UI" forKey:@"iOS"];
NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"iOS"]);
[self SimpleTypes];
// Do any additional setup after loading the view, typically from a nib.
}
#pragma mark---------------簡(jiǎn)單數(shù)據(jù)寫入、讀取----------
- (void)SimpleTypes{
//拼接寫入文件的文件路徑
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
NSString *filepath = [documentsPath stringByAppendingPathComponent:@"file.txt"];
//吧String寫入文件
NSString *string = @"老衲是文字";
BOOL isWriting = [string writeToFile:filepath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (isWriting) {
NSLog(@"成功");
}else{
NSLog(@"失敗");
}
//讀取數(shù)據(jù)
NSString *readString = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",readString);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
打印結(jié)果

屏幕快照 2016-06-24 下午8.18.19.png