IOS 文件系统
2016-02-19 20:14
519 查看
一、文件目录
ios中每个应用程序都是一个沙盒,有自己的沙盒目录,app之间无法访问对方的沙盒文件。
三种沙盒目录:
library:系统存放文件
documents:存放长期使用的文件
temp:临时目录,app重启时,该目录下的文件清空
获取沙盒目录,有两种方式:
1、手动拼接
NSString *homePath = NSHomeDirectory();
NSLog(@"沙盒目录:%@",homePath);
2、直接使用api
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *array1 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
二、文件操作
文件操作涉及到两个类
1、NSFileManager(用于文件的创建、复制、剪切、删除)
//------------------创建文件/文件夹
//获取沙盒目录
NSString *homePath = NSHomeDirectory();
//在沙盒目录中创建一个文件file.text
NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
//NSFileManager是单利模式,所以不能使用alloc+init创建
NSFileManager *manager = [NSFileManager defaultManager];
NSString *str = @"无线互联";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//参数:文件路径、文件内容、文件的属性
BOOL sucess = [manager createFileAtPath:filePath contents:data attributes:nil];
if(sucess){
NSLog(@"文件创建成功");
}else{
NSLog(@"文件创建失败");
}
//创建文件夹
NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/file"];
NSError *error;
//需要传递一个创建失败的指针对象,记录创建失败的信息
BOOL success1 = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:YES attributes:nil error:&error];
if(!success1){
NSLog(@"创建成功");
}else{
NSLog(@"创建失败");
}
//--------------------读取文件
//根据路径读取文件内容
NSData *datas = [manager contentsAtPath:filePath];
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",s);
//--------------------移动文件/剪切文件
//NSFileManager中没有提供重命名的方法,所以我们可以借助移动的api进行操作
//把filePath移动到targetPath目录中
NSString *targetPath = [homePath stringByAppendingPathComponent:@"Documents/file/file2.text"];
BOOL sucess2 = [manager moveItemAtPath:filePath toPath:targetPath error:nil];
if(sucess2) {
NSLog(@"移动成功");
}else{
NSLog(@"移动失败");
}
//--------------------复制文件
BOOL sucess3 = [manager copyItemAtPath:filePath toPath:targetPath error:nil];
if(sucess3){
//复制成功
}else{
//复制失败
}
//--------------------删除文件
//删除之前需要判断这个文件是否存在
BOOL isExist = [manager fileExistsAtPath:filePath];//判断文件是否存在
if(isExist){
BOOL sucess4 = [manager removeItemAtPath:filePath error:nil];
if(sucess4){
//删除成功
}else{
//删除失败
}
}
//--------------------获取文件的属性
NSDictionary *dic = [manager attributesOfItemAtPath:filePath error:nil];
NSLog(@"%@",dic);//通过打印我们就可以查看文件属性的一些key
2、NSFileHandle(对文件进行读写操作)
//1.-------------------字符串读写文件
NSString *str = @"无线互联";
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
//现在有这样的场景,第一次把字符串写入到文件中,当我们修改字符串之后,再次写入的时候,但是可能会写入失败
//但是之前的内容也有可能丢失,因为每次在写入新的内容的时候,会剪切之前的内容,所以这里就有可能新的没有写
//成功,旧的文件也丢失了
//所以这时候atomically参数:
//YES:会将新内容先写入到一个缓存文件中,如果写入缓存成功之后,这时候就将这个缓存文件替换旧文件,这样就很安全了
BOOL sucess = [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if(sucess){
//写入成功
}else{
//写入失败
}
//读取文件内容到字符串中
//类方法
NSString *str1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//构造方法
//str1 = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str1);
//2.--------------------NSData读写
//创建NSData的同时读取文件中的内容
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
//NSData转化成NSString
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",s);
//写文件
BOOL sucess1 = [data writeToFile:filePath atomically:YES];
if(sucess1){
//写入成功
}else{
//写入失败
}
//3.--------------------NSArray读写文件
NSArray *array = @[@"zhangsan",@"lisi"];
//属性文件一般后缀名为.plist
NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/array.plist"];
BOOL sucess2 = [array writeToFile:filePaths atomically:YES];
if(sucess2){
//写入成功
}else{
//写入失败
}
//读文件
NSArray *arrays = [[NSArray alloc] initWithContentsOfFile:filePath];
NSLog(@"%@",arrays);
//4.---------------------NSDictionary读写文件
NSDictionary *dic = @{@"zhang":@"zhangsan",@"li":@"lisi"};
BOOL sucess3 = [dic writeToFile:filePath atomically:YES];
if(sucess3){
//写入成功
}else{
//写入失败
}
//读文件
dic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSLog(@"%@",dic);
//追加数据
NSString *str = @"无线互联";
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"file.test"];
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];//写入文件
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
//默认是从开始位置写,所以我们需要将写入游标设置到尾部
//从文件的末尾写入
[handle seekToEndOfFile];
NSString *s = @"123";
NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];
[handle writeData:data];
//关闭文件
[handle closeFile];
//读取文件
NSFileHandle *handles = [NSFileHandle fileHandleForReadingAtPath:filePath];
//获取文件的大小
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttr = [fileManager attributesOfItemAtPath:filePath error:nil];
NSNumber *fileSize = [fileAttr objectForKey:NSFileSize];
long long sizeValue = [fileSize longLongValue];
//设置偏移量
[handle seekToFileOffset:sizeValue/2];//将偏移量设置到中间位置
//从当前偏移量读到文件末尾
NSData *datas = [handle readDataToEndOfFile];
NSString *s2 = [[NSString alloc] initWithData:datas encoding:NSUTF8StringEncoding];
NSLog(@"%@",s2);
//实现复制文件的功能
//使用NSFileHandle只能读写已经存在的文件,不能创建文件,创建文件应该使用NSFileManager
NSString *targetPath = [homePath stringByAppendingPathComponent:@"files.test"];
NSFileManager *fileManagers = [NSFileManager defaultManager];
[fileManagers createFileAtPath:targetPath contents:nil attributes:nil];
//创建读取文件的handle
NSFileHandle *readHandles = [NSFileHandle fileHandleForReadingAtPath:filePath];
//创建写文件的handle
NSFileHandle *writeHandles = [NSFileHandle fileHandleForWritingAtPath:targetPath];
//从当前偏移量读取到文件的末尾
NSData *datass = [readHandles readDataToEndOfFile];
//还有一种方式读取文件,既可以读取文件,也可以读流,功能更强
//[readHandles availableData];
[writeHandles writeData:datass];
//关闭文件
[readHandles closeFile];
[writeHandles closeFile];
//这里有问题,就是读取文件的时候全部读取了,这样会很占内存的,所以我们应该将读取内容进行分段
ios中每个应用程序都是一个沙盒,有自己的沙盒目录,app之间无法访问对方的沙盒文件。
三种沙盒目录:
library:系统存放文件
documents:存放长期使用的文件
temp:临时目录,app重启时,该目录下的文件清空
获取沙盒目录,有两种方式:
1、手动拼接
NSString *homePath = NSHomeDirectory();
NSLog(@"沙盒目录:%@",homePath);
2、直接使用api
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSArray *array1 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
二、文件操作
文件操作涉及到两个类
1、NSFileManager(用于文件的创建、复制、剪切、删除)
//------------------创建文件/文件夹
//获取沙盒目录
NSString *homePath = NSHomeDirectory();
//在沙盒目录中创建一个文件file.text
NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
//NSFileManager是单利模式,所以不能使用alloc+init创建
NSFileManager *manager = [NSFileManager defaultManager];
NSString *str = @"无线互联";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//参数:文件路径、文件内容、文件的属性
BOOL sucess = [manager createFileAtPath:filePath contents:data attributes:nil];
if(sucess){
NSLog(@"文件创建成功");
}else{
NSLog(@"文件创建失败");
}
//创建文件夹
NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/file"];
NSError *error;
//需要传递一个创建失败的指针对象,记录创建失败的信息
BOOL success1 = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:YES attributes:nil error:&error];
if(!success1){
NSLog(@"创建成功");
}else{
NSLog(@"创建失败");
}
//--------------------读取文件
//根据路径读取文件内容
NSData *datas = [manager contentsAtPath:filePath];
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",s);
//--------------------移动文件/剪切文件
//NSFileManager中没有提供重命名的方法,所以我们可以借助移动的api进行操作
//把filePath移动到targetPath目录中
NSString *targetPath = [homePath stringByAppendingPathComponent:@"Documents/file/file2.text"];
BOOL sucess2 = [manager moveItemAtPath:filePath toPath:targetPath error:nil];
if(sucess2) {
NSLog(@"移动成功");
}else{
NSLog(@"移动失败");
}
//--------------------复制文件
BOOL sucess3 = [manager copyItemAtPath:filePath toPath:targetPath error:nil];
if(sucess3){
//复制成功
}else{
//复制失败
}
//--------------------删除文件
//删除之前需要判断这个文件是否存在
BOOL isExist = [manager fileExistsAtPath:filePath];//判断文件是否存在
if(isExist){
BOOL sucess4 = [manager removeItemAtPath:filePath error:nil];
if(sucess4){
//删除成功
}else{
//删除失败
}
}
//--------------------获取文件的属性
NSDictionary *dic = [manager attributesOfItemAtPath:filePath error:nil];
NSLog(@"%@",dic);//通过打印我们就可以查看文件属性的一些key
2、NSFileHandle(对文件进行读写操作)
//1.-------------------字符串读写文件
NSString *str = @"无线互联";
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
//现在有这样的场景,第一次把字符串写入到文件中,当我们修改字符串之后,再次写入的时候,但是可能会写入失败
//但是之前的内容也有可能丢失,因为每次在写入新的内容的时候,会剪切之前的内容,所以这里就有可能新的没有写
//成功,旧的文件也丢失了
//所以这时候atomically参数:
//YES:会将新内容先写入到一个缓存文件中,如果写入缓存成功之后,这时候就将这个缓存文件替换旧文件,这样就很安全了
BOOL sucess = [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if(sucess){
//写入成功
}else{
//写入失败
}
//读取文件内容到字符串中
//类方法
NSString *str1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
//构造方法
//str1 = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",str1);
//2.--------------------NSData读写
//创建NSData的同时读取文件中的内容
NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
//NSData转化成NSString
NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",s);
//写文件
BOOL sucess1 = [data writeToFile:filePath atomically:YES];
if(sucess1){
//写入成功
}else{
//写入失败
}
//3.--------------------NSArray读写文件
NSArray *array = @[@"zhangsan",@"lisi"];
//属性文件一般后缀名为.plist
NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/array.plist"];
BOOL sucess2 = [array writeToFile:filePaths atomically:YES];
if(sucess2){
//写入成功
}else{
//写入失败
}
//读文件
NSArray *arrays = [[NSArray alloc] initWithContentsOfFile:filePath];
NSLog(@"%@",arrays);
//4.---------------------NSDictionary读写文件
NSDictionary *dic = @{@"zhang":@"zhangsan",@"li":@"lisi"};
BOOL sucess3 = [dic writeToFile:filePath atomically:YES];
if(sucess3){
//写入成功
}else{
//写入失败
}
//读文件
dic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
NSLog(@"%@",dic);
//追加数据
NSString *str = @"无线互联";
NSString *homePath = NSHomeDirectory();
NSString *filePath = [homePath stringByAppendingPathComponent:@"file.test"];
[str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];//写入文件
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
//默认是从开始位置写,所以我们需要将写入游标设置到尾部
//从文件的末尾写入
[handle seekToEndOfFile];
NSString *s = @"123";
NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];
[handle writeData:data];
//关闭文件
[handle closeFile];
//读取文件
NSFileHandle *handles = [NSFileHandle fileHandleForReadingAtPath:filePath];
//获取文件的大小
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttr = [fileManager attributesOfItemAtPath:filePath error:nil];
NSNumber *fileSize = [fileAttr objectForKey:NSFileSize];
long long sizeValue = [fileSize longLongValue];
//设置偏移量
[handle seekToFileOffset:sizeValue/2];//将偏移量设置到中间位置
//从当前偏移量读到文件末尾
NSData *datas = [handle readDataToEndOfFile];
NSString *s2 = [[NSString alloc] initWithData:datas encoding:NSUTF8StringEncoding];
NSLog(@"%@",s2);
//实现复制文件的功能
//使用NSFileHandle只能读写已经存在的文件,不能创建文件,创建文件应该使用NSFileManager
NSString *targetPath = [homePath stringByAppendingPathComponent:@"files.test"];
NSFileManager *fileManagers = [NSFileManager defaultManager];
[fileManagers createFileAtPath:targetPath contents:nil attributes:nil];
//创建读取文件的handle
NSFileHandle *readHandles = [NSFileHandle fileHandleForReadingAtPath:filePath];
//创建写文件的handle
NSFileHandle *writeHandles = [NSFileHandle fileHandleForWritingAtPath:targetPath];
//从当前偏移量读取到文件的末尾
NSData *datass = [readHandles readDataToEndOfFile];
//还有一种方式读取文件,既可以读取文件,也可以读流,功能更强
//[readHandles availableData];
[writeHandles writeData:datass];
//关闭文件
[readHandles closeFile];
[writeHandles closeFile];
//这里有问题,就是读取文件的时候全部读取了,这样会很占内存的,所以我们应该将读取内容进行分段
相关文章推荐
- iOS学习笔记(一)---NSMutableString可变数组
- iOS7后 导航栏中的 translucent 导致的视图frame的变化
- 蓝懿IOS微博项目实现登录
- iOS 运行发现,view随着屏幕的变大整体放大的问题
- iOS旧项目升级WatchOS2.0时遇到的问题
- iOS开发-编译使用动态库
- iOS开发证书都显示“此证书的签发者无效”,更新WWDR Certificate证书后还是显示无效
- iOS OC内存管理、ARC、property属性、__strong、__weak、__block——iOS 编码复习(一)
- ios博客浏览工具
- iOS9设置启动图方法
- iOS启动图片适配问题
- iOS 9之后两个应用的相互调用
- Mos_Tec Tutorial : 001 - iOS视频录制
- 制作framework库文件的详细步骤---iOS9,Xcode7.2
- iOS解析XML
- iOS Collectionview滚动到指定项
- iOS_GCDAsynSocket使用
- iOS(CoreGraphics)画带箭头的框
- iOS 原生二维码扫描(可限制扫描区域)
- ios布局-与xib结合