一、前言
在我们平时的开发过程中,经常会遇到数据本地化的需求,像图片处理、个人信息处理等,这时候我们一般会进行文件操作,将数据存入"沙盒"。
二、沙盒机制
iOS每个应用都有自己对应的沙盒,每个应用程序之间不能相互访问非本程序的沙盒,应用沙盒就是文件系统目录,会自动创建
沙盒包含3个文件夹
- 1、Documents: 程序创建或应用浏览产生的文件数据
- 2、Library:程序的默认设置或状态信息保存在该目录
- Caches : 存储应用程序再次启动所需的信息
- Preferences: 包含应用程序的偏好设置文件
- 3、tmp:提供一个即时创建临时文件的地方,但不需要持久化
三、文件操作方法
- 路径获取
// 获取沙盒路径
- (NSString *)getHomePath {
NSString *homePath = NSHomeDirectory();
return homePath;
}
// 获取Documents路径
- (NSString *)getDocumentsPath {
// 检索指定路径
//参数1-搜索的路径名称
//参数2-限定了在沙盒内部
NSArray *docPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = docPaths.firstObject;
return documentPath;
}
// 获取Library路径
- (NSString *)getLibraryPath {
NSArray *libPaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryPath = libPaths.firstObject;
return libraryPath;
}
// 获取Cache路径
- (NSString *)getCachePath {
NSArray *cachePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = cachePaths.firstObject;
return cachePath;
}
// 获取Tmp路径
- (NSString *)getTmpPath {
NSString *tmpPath = NSTemporaryDirectory();
return tmpPath;
}
- 路径函数处理
// 路径函数处理
- (void)parsePath {
NSString *path = @"/Library/Developer/CoreSimulator/Devices/test.png";
// 获得路径的各个组成部分
NSArray *array = [path pathComponents];
// 提取路径最后一个组成部分
NSString *lastName = [path lastPathComponent];
// 删除路径最后一个组成部分
NSString *delPath = [path stringByDeletingLastPathComponent];
// 添加文件
NSString *addStr = [path stringByAppendingPathComponent:@"area.plist"];
}
- NSData 转换
// NSData 数据转换
- (void)dataChange:(NSData *)data {
// NSString类型
// NSData -> NSString
NSString *aString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSString -> NSData
NSData *aData = [aString dataUsingEncoding:NSUTF8StringEncoding];
// UIImage类型
// NSData -> UIImage
UIImage *image = [UIImage imageWithData:data];
//UIImage -> NSData
NSData *imageData1 = UIImagePNGRepresentation(image);
NSData *imageData2 = UIImageJPEGRepresentation(image, 1);
}
- 文件操作
1、NSFileHandle类主要对文件内容进行读取和写入操作
2、NSFileManager类主要对文件的操作(删除、创建等)
// 创建文件夹
- (void)createFolder {
NSString *docPath = [self getDocumentsPath];
NSString *folderName = [docPath stringByAppendingPathComponent:@"note"];
NSFileManager *manager = [NSFileManager defaultManager];
// 创建文件夹
// path 文件路径
// withIntermediateDirectories: YES 如果文件夹存在可以覆盖 NO 不可覆盖
BOOL isSuccess = [manager createDirectoryAtPath:folderName withIntermediateDirectories:YES attributes:nil error:nil];
if (isSuccess){
NSLog(@"文件夹创建成功!");
}else {
NSLog(@"文件夹创建失败!");
}
}
// 创建文件
- (void)createFile {
NSString *docPath = [self getDocumentsPath];
NSString *testPath = [docPath stringByAppendingPathComponent:@"note"];
NSString *filePath = [testPath stringByAppendingPathComponent:@"note.txt"];
NSFileManager *manager = [NSFileManager defaultManager];
// 创建文件
// contents: NSData 类型,可将UIImage、NSString转化后存入
BOOL isSuccess = [manager createFileAtPath:filePath contents:nil attributes:nil];
if (isSuccess){
NSLog(@"文件创建成功!");
}else {
NSLog(@"文件创建失败!");
}
}
// 删除文件
- (void)deleteFile {
NSString *docPath = [self getDocumentsPath];
NSString *testPath = [docPath stringByAppendingPathComponent:@"note"];
NSString *filePath = [testPath stringByAppendingPathComponent:@"note.txt"];
NSFileManager *manager = [NSFileManager defaultManager];
// 检测文件是否存在
BOOL isExit = [self fileExist:filePath];
if (isExit){
// 删除文件
BOOL isSuccess = [manager removeItemAtPath:filePath error:nil];
if (isSuccess) {
NSLog(@"文件删除成功!");
}else {
NSLog(@"文件删除失败!");
}
}
}
// 检测文件是否存在
- (BOOL)fileExist:(NSString *)filePath {
NSFileManager *manager = [NSFileManager defaultManager];
if ([manager fileExistsAtPath:filePath]){
return YES;
}else {
return NO;
}
}
// 写入文件内容
- (void)writeFile {
NSString *docPath = [self getDocumentsPath];
NSString *testPath = [docPath stringByAppendingPathComponent:@"note"];
NSString *filePath = [testPath stringByAppendingPathComponent:@"note.txt"];
NSString *content = @"我的笔记";
BOOL isSuccess = [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (isSuccess){
NSLog(@"写入文件成功!");
}else {
NSLog(@"写入文件失败!");
}
}
// 追加内容
- (void)addFile {
NSString *docPath = [self getDocumentsPath];
NSString *testPath = [docPath stringByAppendingPathComponent:@"note"];
NSString *filePath = [testPath stringByAppendingPathComponent:@"note.txt"];
// 打开文件、准备更新
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
// 将节点跳转到文件的末尾
[fileHandle seekToEndOfFile];
NSString *string = @"这是要添加的内容";
NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];
// 写入内容
[fileHandle writeData:stringData];
// 最后要关闭文件
[fileHandle closeFile];
}
//获取文件内容
- (NSData *)getFileData:(NSString *)filePath
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *fileData = [handle readDataToEndOfFile];
[handle closeFile];
return fileData;
}
//获取文件大小
- (long long)getFileSizeWithPath:(NSString *)path {
unsigned long long fileLength = 0;
NSNumber *fileSize;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
if ((fileSize = [fileAttributes objectForKey:NSFileSize])) {
fileLength = [fileSize unsignedLongLongValue]; //单位是 B
}
return fileLength / 1000; //换算为K
}
//获取文件创建时间
- (NSString *)getFileCreatDateWithPath:(NSString *)path {
NSString *date = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
date = [fileAttributes objectForKey:NSFileCreationDate];
return date;
}
//获取文件更改日期
- (NSString *)getFileChangeDateWithPath:(NSString *)path {
NSString *date = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
date = [fileAttributes objectForKey:NSFileModificationDate];
return date;
}
四、总结
本文只是做个笔记,方便以后查阅,如有不足之处请指正
参考文档
网友评论