- 获取应用沙盒根目录
+(NSString *)YYHomeDirectory{
return NSHomeDirectory();
}
//获取Documents目录
+(NSString *)YYDocumentsDirectory{
//[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
//获取Tmp目录
+(NSString *)YYTmpDirectory{
//[NSHomeDirectory() stringByAppendingPathComponent:@"tmp"];
NSString *tmpDirectory = NSTemporaryDirectory();
return tmpDirectory;
}
//获取Cache目录
+(NSString *)YYCacheDirectory{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
return cacheDirectory;
}
//获取Library目录
+(NSString *)YYLibraryDirectory{
//[NSHomeDirectory() stringByAppendingPathComponent:@"Library"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
return libraryDirectory;
}
#pragma mark --Documents下的文件操作
//得到Documents里的文件路径
+ (NSString *)getFilePathAtDocuments:(NSString *)fileName{
return [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
}
//删除Documents里的文件
+ (BOOL)deleteFileAtDocuments:(NSString *)fileName{
NSString *filePath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:filePath])
{
return NO;
}
[fileManager removeItemAtPath:filePath error:nil];
return YES;
}
//创建指定名字的文件
+ (BOOL)createFileAtDocuments:(NSString *)fileName{
NSString *filePath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:filePath]){
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
return YES;
}
return NO;
}
//创建指定名字的文件夹
+ (BOOL)createDirectoryAtDocuments:(NSString *)fileName{
NSString *filePath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:filePath]){
NSError *error = nil;
[fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:&error];
return YES;
}
return NO;
}
//文件是否存在
+ (BOOL)isFileExistsAtDocuments:(NSString *)fileName{
NSString *filePath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:filePath]){
return NO;
}
return YES;
}
//写文件
+(BOOL)writeFileAtDocumentsWithName:(NSString *) fileName AndContent:(NSString *)content{
NSString *iOSPath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
BOOL isSuccess = [content writeToFile:iOSPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
return isSuccess;
}
//读文件
+ (NSString*)readFileContentAtDocumentsWithName:(NSString*)fileName{
NSString *filePath = [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
return content;
}