下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
- (Nsstring *)getDocumentsPath
{
//获取Documents路径
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
Nsstring *path = [paths objectAtIndex:0];
NSLog(@"path:%@",path);
return path;
}
创建文件夹
-(void)createDirectory{
Nsstring *documentsPath =[self getDocumentsPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
Nsstring *iOSDirectory = [documentsPath stringByAppendingPathComponent:@"iOS"];
BOOL isSuccess = [fileManager createDirectoryAtPath:iOSDirectory withIntermediateDirectories:YES attributes:nil error:nil];
if (isSuccess) {
NSLog(@"success");
} else {
NSLog(@"fail");
}
}
创建文件
-(void)createFile{
Nsstring *documentsPath =[self getDocumentsPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
Nsstring *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
BOOL isSuccess = [fileManager createFileAtPath:iOSPath contents:nil attributes:nil];
if (isSuccess) {
NSLog(@"success");
} else {
NSLog(@"fail");
}
}
写文件
-(void)writeFile{
Nsstring *documentsPath =[self getDocumentsPath];
Nsstring *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
Nsstring *content = @"我要写数据啦";
BOOL isSuccess = [content writetoFile:iOSPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (isSuccess) {
NSLog(@"write success");
} else {
NSLog(@"write fail");
}
}
读取文件内容
-(void)readFileContent{
Nsstring *documentsPath =[self getDocumentsPath];
Nsstring *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
Nsstring *content = [Nsstring stringWithContentsOfFile:iOSPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"read success: %@",content);
}
判断文件是否存在
- (BOOL)isSxistAtPath:(Nsstring *)filePath{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isExist = [fileManager fileExistsAtPath:filePath];
return isExist;
}
计算文件大小
- (unsigned long long)fileSizeAtPath:(Nsstring *)filePath{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isExist = [fileManager fileExistsAtPath:filePath];
if (isExist){
unsigned long long fileSize = [[fileManager attributesOfItemAtPath:filePath error:nil] fileSize];
return fileSize;
} else {
NSLog(@"file is not exist");
return 0;
}
}
计算整个文件夹中所有文件大小
- (unsigned long long)folderSizeAtPath:(Nsstring*)folderPath{
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL isExist = [fileManager fileExistsAtPath:folderPath];
if (isExist){
NSEnumerator *childFileEnumerator = [[fileManager subpathsAtPath:folderPath] objectEnumerator];
unsigned long long folderSize = 0;
Nsstring *fileName = @"";
while ((fileName = [childFileEnumerator nextObject]) != nil){
Nsstring* fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];
folderSize += [self fileSizeAtPath:fileAbsolutePath];
}
return folderSize / (1024.0 * 1024.0);
} else {
NSLog(@"file is not exist");
return 0;
}
}
删除文件
-(void)deleteFile{
Nsstring *documentsPath =[self getDocumentsPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
Nsstring *iOSPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
BOOL isSuccess = [fileManager removeItemAtPath:iOSPath error:nil];
if (isSuccess) {
NSLog(@"delete success");
}else{
NSLog(@"delete fail");
}
}
移动文件
- (void)moveFileName
{
Nsstring *documentsPath =[self getDocumentsPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
Nsstring *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
Nsstring *movetoPath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:movetoPath error:nil];
if (isSuccess) {
NSLog(@"rename success");
}else{
NSLog(@"rename fail");
}
}
重命名
- (void)renameFileName
{
//通过移动该文件对文件重命名
Nsstring *documentsPath =[self getDocumentsPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
Nsstring *filePath = [documentsPath stringByAppendingPathComponent:@"iOS.txt"];
Nsstring *movetoPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"];
BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:movetoPath error:nil];
if (isSuccess) {
NSLog(@"rename success");
}else{
NSLog(@"rename fail");
}
}
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。