- NSFileManager:是用來管理文件系統(tǒng)的,它可以用來進行常見的文件\文件夾操作
- 獲取NSFileManager示例
[NSFileManager defaultManager]
增刪改查
1. 創(chuàng)建文件夾
- (void)createFolder {
// 獲取documentsPath
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// 文件夾路徑
NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"];
NSError *error;
BOOL temp = [[NSFileManager defaultManager] createDirectoryAtPath:folderPath withIntermediateDirectories:YES attributes:nil error:&error];
if (temp) {
NSLog(@"文件夾創(chuàng)建成功:%@", folderPath);
} else {
NSLog(@"文件夾創(chuàng)建失敗\n失敗原因:%@", error);
}
}
2. 創(chuàng)建文件
- (void)createFile {
// 獲取documentsPath
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// 設(shè)置文件夾路徑
NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"];
NSString *testPath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"];
BOOL temp = [[NSFileManager defaultManager] createFileAtPath:testPath contents:nil attributes:nil];
if (temp) {
NSLog(@"文件創(chuàng)建成功:%@", testPath);
} else {
NSLog(@"文件創(chuàng)建失敗");
}
}
3. 向文件中寫入數(shù)據(jù)
- (void)writeDataToFile {
// 獲取documentsPath
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// 文件夾路徑
NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"];
// 文件路徑
NSString *filePath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"];
NSString *content = @"hello world";
NSError *error;
BOOL temp = [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
if (temp) {
NSLog(@"文件寫入成功:%@", filePath);
} else {
NSLog(@"文件寫入失敗\n失敗原因:%@", error);
}
}
4. 刪除文件
- (void)deleteFile {
// 獲取documentsPath
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// 文件夾路徑
NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"];
// 文件路徑
NSString *filePath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"];
NSError *error;
BOOL temp = [[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
if (temp) {
NSLog(@"文件刪除成功");
} else {
NSLog(@"文件刪除失敗\n失敗原因:%@", error);
}
}
5. 從文件中讀取數(shù)據(jù)
- (void)readFile {
// 獲取documentsPath
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
// 文件夾路徑
NSString *folderPath = [documentsPath stringByAppendingPathComponent:@"selfFolder"];
// 文件路徑
NSString *filePath = [folderPath stringByAppendingPathComponent:@"selfFile.txt"];
// NSData *data = [NSData dataWithContentsOfFile:filePath];
NSError *error;
NSString *content = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (!error) {
NSLog(@"文件讀取成功:%@", content);
} else {
NSLog(@"文件寫入失敗\n失敗原因:%@", error);
}
}
常用工具方法
1. 判斷文件是否存在
[[NSFileManager defaultManager] fileExistsAtPath:filePath]
2. 判斷是否為一個目錄
[[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir];
3. 判斷文件是否可讀
[[NSFileManager defaultManager] isReadableFileAtPath:filePath];
4. 是否可寫
[[NSFileManager defaultManager] isWritableFileAtPath:filePath];
5. 是否可刪除
[[NSFileManager defaultManager] isDeletableFileAtPath:filePath];
6. 獲取文件屬性
NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
7. copy文件
[[NSFileManager defaultManager] copyItemAtPath:path1 toPath:path2 error:nil];
8. 移動文件
[[NSFileManager defaultManager] moveItemAtPath:createDirPath toPath:targetPath error:nil];
GitHub主頁
CSDN Blog
Email:jinjob@icloud.com