1.新建类 “sandBox”
在sandBox.h中
//写接口
pragma mark --- (封装沙河路径方法)
//获取document文件目录
+(NSString *)getDocumentDirectory;
//获取library目录
+(NSString *)getLibraryDirectionary;
//获取library/Caches目录
+(NSString *)getCachesDirectionary;
//获取library/Preferences
+(NSString *)getPreferencesDirectory;
//获取temp文件路径
+(NSString *)getTempDirectory;
//获取xxxx.app文件路径
+(NSString *)getAppPath;
在sandBox.m中
pragma mark --- (封装沙河路径方法)
//获取document文件目录
//将程序当中建立的或者在程序中浏览的数据保存在这
+(NSString *)getDocumentDirectory{
/*
三个参数:
第一个参数:枚举(具体要查看的某个文件目录【进入的文件夹】)
第二个参数: NSUserDomainMask用户主目录
第三个参数:YES 设置为表示展开完整路径
NSSearchPathForDirectoriesInDomains代表查找沙盒路径,返回值是一个数组,该书中当中只有一个元素,这个元素就是路径
*/
NSString * documentPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)firstObject];
return documentPath;
}
//获取library目录
//保存的是程序默认设置,或者一些状态信息
+(NSString *)getLibraryDirectionary{
return [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];
}
//获取library/Caches目录
//存放地是缓存文件。。音频视频
+(NSString *)getCachesDirectionary{
return [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)lastObject];
}
//获取library/Preferences
//偏好设置文件app 偏好设置存储在这里,例如:是否是否访问图片,是否访问地理位置
+(NSString *)getPreferencesDirectory{
//获取到library/Preferences目录
NSArray *path2= NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
//是将前面路径格式和后面的字符串给拼接在一起
//并且已路径格式返回
NSString * path1=[[path2 objectAtIndex:0]stringByAppendingString:@"Preference"];
return path1;
//return [NSSearchPathForDirectoriesInDomains(NSPreferencePanesDirectory, NSUserDomainMask, YES)];
}
//获取temp文件路径
+(NSString *)getTempDirectory{
return NSTemporaryDirectory();
}
//获取xxxx.app文件路径
+(NSString *)getAppPath{
return [[NSBundle mainBundle]resourcePath];
}
- ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
#warning --- 简单对象的读写:
//简单对象:《字符串/数组/字典/数据(data)》
[self writefile];
[self myFileManager];
}
pragma mark--- 文件管理 ---创建---
-(void)myFileManager{
//初始化NSFileManager
//defaultManager方法为单例方法
NSFileManager * manager = [NSFileManager defaultManager];
//获取跟路径
NSString * path = NSHomeDirectory();
path = [path stringByAppendingPathComponent:@"text/myApp"];
//创建目录
[manager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
//沙盒中创建文件
// NSString * path2=[path stringByAppendingString:@"text2/App"];
//
// [manager createDirectoryAtPath:path2 withIntermediateDirectories:YES attributes:nil error:nil];
// NSLog(@"%@",path);
pragma mark ---文件管理 ---添加:
path =[path stringByAppendingPathComponent:@"fuchuan.txt"];
// NSLog(@"%@",path);
//写入内容
NSString * string = @"我是向日葵";
//将文本写入txt中
BOOL result=[string writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (result){
NSLog(@"写入成功%@",path);
}
else{
NSLog(@"失败");
}
}
warning --- 简单对象的读写:
-(void)writefile{
/*
//字符串写入
//首先获取document文件路径
NSArray * documentArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//获取首元素
NSString * documentpath=documentArray[0];
//文件夹名字<字符串.txt>
documentpath=[documentpath stringByAppendingString:@"/zifuchuan.txt"];
//NSLog(@"%@",documentpath);
//文本内容
NSString *string =@"我有一百种方法让你爱上我,其中一种就是可劲调戏你";
//将文本内容写入的doucument的文件当中
BOOL result =[string writeToFile:documentpath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if(result){
NSLog(@"写入成功,数据就是%@",documentpath);
}
else{
NSLog(@"数据写入失败");
}
*/
/*
//数组写入到沙盒文件中
//获取文件路径
NSString * document=[sandBox getDocumentDirectory];
//在document下拼接文件名;
NSString * path =[document stringByAppendingString:@"array.plist"];
//创建数组
NSArray * arrayFile = @[@"华晨宇",@"吴亦凡",@"宋仲基",@"向日葵"];
//将数组写入到documents文件当中
[arrayFile writeToFile:path atomically:YES];
NSLog(@"****%@",path);
*/
/*
//将字典写入到沙盒文件中
//写字典
//获取document路径
NSString * dicPath = [sandBox getDocumentDirectory];
//在document下拼接文件名;
NSString * path=[dicPath stringByAppendingPathComponent:@"zidian.plist"];
NSDictionary * dict=@{@"name":@"小花"};
//将字典写入到documents文件当中
[dict writeToFile:path atomically:YES];
NSLog(@"%@",path);
*/
//将数据写入到沙盒文件中
//获取路径并创建文件
NSString * dataPath=[[sandBox getDocumentDirectory]stringByAppendingPathComponent:@"data.a"];
//
NSString *datastr = @"阿尔卑斯";
//将string转化为NSData类型
NSData * mydata= [datastr dataUsingEncoding:NSUTF8StringEncoding];
//写入到文件中
[mydata writeToFile:dataPath atomically:YES];
NSLog(@"")
}