// 获取沙盒根路径
+ (NSString *)getHomePath {
return NSHomeDirectory();
}
// 获取tmp路径
+ (NSString *)getTmpPath {
return NSTemporaryDirectory();
}
// 获取Documents路径
+ (NSString *)getDocumentsPath {
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [pathArr firstObject];
return path;
}
// 获取Library路径
+ (NSString *)getLibraryPath {
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path = [pathArr firstObject];
return path;
}
// 获取LibraryCache路径
+ (NSString *)getLibraryCachePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths firstObject];
return path;
}
// 检查文件、文件夹是否存在
+ (BOOL)fileExistsAtPath:(NSString *)path isDirectory:(BOOL *)isDir {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL exist = [fileManager fileExistsAtPath:path isDirectory:isDir];
return exist;
}
// 创建路径
+ (void)createDirectory:(NSString *)path {
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isDir;
BOOL exist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
if (!isDir) {
[fileManager removeItemAtPath:path error:nil];
exist = NO;
}
if (!exist) {
// 注:直接创建不会覆盖原文件夹
[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
}
}
// 创建文件
+ (NSString *)createFile:(NSString *)filePath fileName:(NSString *)fileName {
// 先创建路径
[self createDirectory:filePath];
// 再创建路径上的文件
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = [filePath stringByAppendingPathComponent:fileName];
BOOL isDir;
BOOL exist = [fileManager fileExistsAtPath:path isDirectory:&isDir];
if (isDir) {
[fileManager removeItemAtPath:path error:nil];
exist = NO;
}
if (!exist) {
// 注:直接创建会被覆盖原文件
[fileManager createFileAtPath:path contents:nil attributes:nil];
}
return path;
}
// 复制 文件or文件夹
+ (void)copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL result = [fileManager copyItemAtPath:srcPath toPath:dstPath error:&error];
if (!result && error) {
NSLog(@"copyItem Err : %@", error.description);
}
}
// 移动 文件or文件夹
+ (void)moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dstPath {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL result = [fileManager moveItemAtPath:srcPath toPath:dstPath error:&error];
if (!result && error) {
NSLog(@"moveItem Err : %@", error.description);
}
}
// 删除 文件or文件夹
+ (void)removeItemAtPath:(NSString *)path {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
BOOL result = [fileManager removeItemAtPath:path error:&error];
if (!result && error) {
NSLog(@"removeItem Err : %@", error.description);
}
}
// 获取目录下所有内容
+ (NSArray *)getContentsOfDirectoryAtPath:(NSString *)docPath {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *contentArr = [fileManager contentsOfDirectoryAtPath:docPath error:&error];
if (!contentArr.count && error) {
NSLog(@"ContentsOfDirectory Err : %@", error.description);
}
return contentArr;
}
能直接进行文件读写的数据类型
iOS中有四种简单类型能够直接进行文件读写:字符串NSString/NSMutableString、数组NSArray/NSMutableArray、字典NSDictionary/NSMutableDictionary、二进制数据NSData/NSMutableData。
NSString *string = @"QiShare test string ...";
[string writeToFile:path11 atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString *readStr = [NSString stringWithContentsOfFile:path11 encoding:NSUTF8StringEncoding error:nil];
NSLog(@"读取文件-字符串: %@", readStr);
NSArray *array = @[@"Q", @"i", @"S", @"h", @"a", @"r", @"e"];
[array writeToFile:path33 atomically:YES];
NSArray *readArr = [NSArray arrayWithContentsOfFile:path33];
NSLog(@"读取文件-数组: %@", readArr);
NSDictionary *dict = @{@"en":@"QiShare", @"ch":[[FileUtil alloc] init]};
[dict writeToFile:path34 atomically:YES];
NSDictionary *readDict = [NSDictionary dictionaryWithContentsOfFile:path34];
NSLog(@"读取文件-字典: %@", readDict);
NSData *data = [@"QiShare test data ..." dataUsingEncoding:NSUTF8StringEncoding];
[data writeToFile:path11 atomically:YES];
NSData *readData = [NSData dataWithContentsOfFile:path11];
NSLog(@"读取文件-二进制: %@", readData);
// NSFileHandle操作文件内容
- (void)testFileHandle {
NSString *docPath = [FileUtil getDocumentsPath];
NSString *readPath = [docPath stringByAppendingPathComponent:@"read.txt"];
NSString *writePath = [docPath stringByAppendingPathComponent:@"write.txt"];
NSData *data = [@"abcdefghijklmnopqrstuvwxyz" dataUsingEncoding:NSUTF8StringEncoding];
NSFileManager *manager=[NSFileManager defaultManager];
[manager createFileAtPath:readPath contents:data attributes:nil];
[manager createFileAtPath:writePath contents:nil attributes:nil];
[data writeToFile:readPath atomically:YES];
// 打开文件 读
NSFileHandle *readHandle = [NSFileHandle fileHandleForReadingAtPath:readPath];
NSData *readData = [readHandle readDataToEndOfFile];
// 读取文件中指定位置/指定长度的内容
[readHandle seekToFileOffset:10];
readData = [readHandle readDataToEndOfFile];
NSLog(@"seekToFileOffset:10 = %@", [[NSString alloc] initWithData:readData encoding:NSUTF8StringEncoding]);
[readHandle seekToFileOffset:10];
readData = [readHandle readDataOfLength:5];
NSLog(@"seekToFileOffset:10 = %@",[[NSString alloc]initWithData:readData encoding:NSUTF8StringEncoding]);
[readHandle closeFile];
// 打开文件 写
NSFileHandle *writeHandle = [NSFileHandle fileHandleForWritingAtPath:writePath];
// 注:直接覆盖文件原有内容
[writeHandle writeData:data];
// 注:覆盖了指定位置/指定长度的内容
[writeHandle seekToFileOffset:2];
[writeHandle writeData:[@"CDEFG" dataUsingEncoding:NSUTF8StringEncoding]];
[writeHandle seekToEndOfFile];
[writeHandle writeData:[@"一二三四五六" dataUsingEncoding:NSUTF8StringEncoding]];
[writeHandle closeFile];
}