- 获取应用沙盒根目录
+(NSString *)YYHomeDirectory{
return NSHomeDirectory();
}
+(NSString *)YYDocumentsDirectory{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return documentsDirectory;
}
+(NSString *)YYTmpDirectory{
NSString *tmpDirectory = NSTemporaryDirectory();
return tmpDirectory;
}
+(NSString *)YYCacheDirectory{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDirectory = [paths objectAtIndex:0];
return cacheDirectory;
}
+(NSString *)YYLibraryDirectory{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = [paths objectAtIndex:0];
return libraryDirectory;
}
#pragma mark --Documents下的文件操作
+ (NSString *)getFilePathAtDocuments:(NSString *)fileName{
return [[self YYDocumentsDirectory] stringByAppendingPathComponent:fileName];
}
+ (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;
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步