//
// 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();
# #注意每個文件夾的名字,大小寫不能錯
/**
* 每個沙盒路徑下都有三個文件夾
*Documents 是最常用的购裙,可以儲存一些不太大的數(shù)據(jù)析苫,plist、文本潭千、splite谱姓。它會和itunes同步
!
*Library
caches:儲存大容量的數(shù)據(jù)刨晴,caches路徑的文件不會和itunes同步
preference:儲存用戶設(shè)置屉来。(偏好設(shè)置)
tmp 臨時文件夾,不進行同步狈癞,當(dāng)設(shè)備重啟,就會清空茄靠。
*/
// 拼接路徑獲取documents路徑
NSString *path = [sandBoxpath stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@",path);
/**
* 直接獲取路徑
*
* @第一個參數(shù):尋找的哪一個文件夾
* @第二個參數(shù):用戶沙盒主目錄
* @第三個參數(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---------------簡單數(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