// 獲取Documents路徑
- (NSString *)getDocumentPath{
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) firstObject];
return path;
}
// 創(chuàng)建文件夾
- (void)createDirectory{
NSString *documentPath = [self getDocumentPath];
NSFileManager *filemanager = [NSFileManager defaultManager];
NSString *iosDirectory = [documentPath stringByAppendingPathComponent:@"ios"];
BOOL isSuccess = [filemanager createDirectoryAtPath:iosDirectory withIntermediateDirectories:YES attributes:nil error:nil];
if (isSuccess) {
NSLog(@"success");
}else NSLog(@"fail");
}
// 創(chuàng)建文件
- (void)createFile{
NSString *documentsPath = [self getDocumentPath];
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 getDocumentPath];
NSString *iosPath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
NSString *content = @"我是一個數(shù)據(jù)拉";
BOOL isSuccess = [content writeToFile:iosPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (isSuccess) {
NSLog(@"success");
}else{
NSLog(@"fail");
}
}
// 讀取文件內(nèi)容
- (void)readFileContent{
NSString *documentsPath = [self getDocumentPath];
NSString *iosPath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
NSString *content = [NSString stringWithContentsOfFile:iosPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"read success --%@",content);
NSUInteger num = [self fileSizeAtPath:iosPath];
NSLog(@"---%lud",(unsigned long)num);
}
// 判斷文件是否存在
- (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 isExsit = [filemanager fileExistsAtPath:folderPath];
if (isExsit) {
NSEnumerator *chileFileEnumerator = [[filemanager subpathsAtPath:folderPath] objectEnumerator];
unsigned long long folderSize = 0;
NSString *fileName = @"";
while ((fileName = [chileFileEnumerator 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 *documentPath = [self getDocumentPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *iosPath = [documentPath stringByAppendingPathComponent:@"ios.txt"];
BOOL isSuccess = [fileManager removeItemAtPath:iosPath error:nil];
if (isSuccess) {
NSLog(@"delete success");
}else{
NSLog(@"delete fail");
}
}
// 移動文件
- (void)moveFileName{
NSString *documentsPath =[self getDocumentPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"ios1.txt"];
BOOL isSuccess = [fileManager moveItemAtPath:filePath toPath:moveToPath error:nil];
if (isSuccess) {
NSLog(@"move success");
}else{
NSLog(@"move fail");
}
}
// 重命名
- (void)renameFileName{
/*
文件重命名的兩個方法:
方法一、思路:1.提取原有文件data截歉,
2.將data存入新建的文件,
3.刪除原有文件
方法二、用[manager moveItemAtPath:filePath toPath:newPath error:nil];方法將filepath
路徑下的文件名換成newPath
對文件NSData賦值昭抒,在文件內(nèi)將內(nèi)容更改后攘蔽,move后的文件內(nèi)容保持跟最后修改的一致
如果文件NSdata未賦值宋下,在程序外文件內(nèi)更改,move后新文件仍為空篡腌。
*/
// 通過移動文件對文件重命名
NSString *documentsPath = [self getDocumentPath];
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");
}
}
// 文件復(fù)制
- (void)copyFileName{
NSString *documentsPath = [self getDocumentPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"ios.txt"];
NSString *moveToPath = [documentsPath stringByAppendingPathComponent:@"rename.txt"];
if ([fileManager copyItemAtPath:filePath toPath:moveToPath error:nil]) {
NSLog(@"success");
}else{
NSLog(@"fail");
}
}