- (void)viewDidLoad {
[super viewDidLoad];
NSFileManager *fileManager = [NSFileManager defaultManager];
#pragma mark - 目錄的操作
// 目錄
NSArray *fileArr = [fileManager contentsOfDirectoryAtPath:@"/Users/jessy/Desktop/tab" error:nil];
NSLog(@"%@",fileArr);
for (NSString *detail in fileArr) {
NSLog(@"detail---%@",detail);
}
// 詳細(xì)到每個子目錄,,
NSArray *subArr = [fileManager subpathsOfDirectoryAtPath:@"/Users/jessy/Desktop/tab" error:nil];
NSLog(@"%@",subArr);
for (NSString *subDetail in subArr) {
NSLog(@"subDetail---%@",subDetail);
}
// 創(chuàng)建目錄
BOOL createDir = [fileManager createDirectoryAtPath:@"/Users/jessy/Desktop/tab/23/12" withIntermediateDirectories:YES attributes:nil error:nil];
[self judgeName:createDir];
#pragma mark - 文件的操作
// 文件的創(chuàng)建與寫入
NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).firstObject;
NSString *filePath = [path stringByAppendingPathComponent:@"fileTest.txt"];
NSString *message = @"ceshia";
if (![fileManager fileExistsAtPath:filePath]) {
BOOL createFile = [fileManager createFileAtPath:filePath contents:[message dataUsingEncoding:NSUTF8StringEncoding ] attributes:nil];
[self judgeName:createFile];
}
// 讀取文件的內(nèi)容
NSData *fileData = [fileManager contentsAtPath:filePath];
NSString *fileStr = [[NSString alloc]initWithData:fileData encoding:NSUTF8StringEncoding];
NSLog(@"%@",fileStr);
// 移動文件
NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES).firstObject;
NSString *newPath = [cachePath stringByAppendingPathComponent:@"fileTest.txt"];
if (![fileManager fileExistsAtPath:newPath]) {
BOOL movePath = [fileManager moveItemAtPath:filePath toPath:newPath error:nil];
[self judgeName:movePath];
}
// 復(fù)制文件
if (![fileManager fileExistsAtPath:filePath]) {
BOOL copyFile = [fileManager copyItemAtPath:newPath toPath:filePath error:nil];
[self judgeName:copyFile];
}
// 比較兩個文件的內(nèi)容
BOOL result = [fileManager contentsEqualAtPath:newPath andPath:filePath];
if (result) {
NSLog(@"一樣");
}else
{
NSLog(@"NO");
}
// 刪除文件
BOOL removeFile = [fileManager removeItemAtPath:filePath error:nil];
[self judgeName:removeFile];
}
- (void)judgeName:(BOOL)name{
if (name) {
NSLog(@"成功");
}else{
NSLog(@"失敗");
}
}
NSFileManager的使用
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 前言瞎扯 我們在做項目過程中難免會接觸到存儲文件這種操作,那么必然會和NSFileManager發(fā)生關(guān)系(你們想歪...
- 由于本人提交app的時候需要修改文件夾的名字,并且給 .m 文件增加函數(shù)名稱,之前一直是手動操作,每次提交app的...
- 寫在前面弄了下個人站...防止內(nèi)容再次被鎖定...所有東西都在這里面welcome~個人博客 所有方法 都很簡單,...