//
// 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 是最常用的,可以储存一些不太大的数据,plist、文本、splite。它会和itunes同步
!
*Library
caches:储存大容量的数据,caches路径的文件不会和itunes同步
preference:储存用户设置。(偏好设置)
tmp 临时文件夹,不进行同步,当设备重启,就会清空。
*/
// 拼接路径获取documents路径
NSString *path = [sandBoxpath stringByAppendingPathComponent:@"Documents"];
NSLog(@"%@",path);
/**
* 直接获取路径
*
* @第一个参数:寻找的哪一个文件夹
* @第二个参数:用户沙盒主目录
* @第三个参数:如果为 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---------------简单数据写入、读取----------
- (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(@"失败");
}
//读取数据
NSString *readString = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",readString);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
打印结果
屏幕快照 2016-06-24 下午8.18.19.png