NSFileManager for iOS SDK
NSFileManager provides a convenient method for file operations, including file and directory creation, copying, cutting, and deletion.
This article will explain in detail how to perform these basic operations.
When using defaultManager, you actually obtain a singleton (same object), which is thread-safe. In most cases, you can use this. This topic describes the basic operations. If it is used in different threads and requires a proxy function to listen to events, you must use init to create an independent fileManager for each thread. Positioning
To put it bluntly, it is to get some directories. There are two main functions.
Just Positioning
- URLsForDirectory:inDomains:
Example
Obtain the library directory (which exists by default)
NSFileManager * fileManager = [NSFileManager defaultManager]; NSArray * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]; NSURL * documentPath = [searchResult firstObject]; NSLog(@%@,documentPath);
You can create
- URLForDirectory:inDomain:appropriateForURL:create:error:
Get Application Support (does not exist by default)
NSFileManager * fileManager = [NSFileManager defaultManager]; NSURL * path = [fileManager URLForDirectory:NSApplicationSupportDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil]; NSLog(@%@,path);
Several common parameters mentioned here
NSLibraryDirectory-Library directory nsapplicationsuppsuppdirectory-Library/Application Support directory NSDocumentDirectory-Document directory NSUserDomainMask-User Domain
For more information about what domain is stored, see my previous article on sandbox.
Http://blog.csdn.net/hello_hwc/article/details/44916909 Determine whether a file/directory exists
Two Functions
The second function has an additional output. If the file exists, it will show whether the file is a directory file.
- fileExistsAtPath:- fileExistsAtPath:isDirectory:
NSFileManager * fileManager = [NSFileManager defaultManager]; NSArray * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]; NSURL * documentPath = [searchResult firstObject]; NSString * newPath = [documentPath.path stringByAppendingPathComponent:@Demo/Wenchen]; if ([fileManager fileExistsAtPath:newPath] == false) { NSLog(@Path not exist); } BOOL isDic; if ([fileManager fileExistsAtPath:documentPath.path isDirectory:&isDic] == false) { NSLog(@Path not exist); } NSLog(@%d,isDic);
Create
Create directory
The two function parameters are similar, but the type of the first parameter is different.
-createDirectoryAtURL:withIntermediateDirectories:attributes:error:- createDirectoryAtPath:withIntermediateDirectories:attributes:error:
Return Bool to indicate whether the operation is successful. If an error occurs, the error message is in error.
The second parameter indicates whether to automatically create a parent directory that does not exist (that is, to create a multi-level directory at a time)
The third parameter is used to set access permissions, usually nil.
Example
NSFileManager * fileManager = [NSFileManager defaultManager]; NSArray * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]; NSURL * documentPath = [searchResult firstObject]; NSString * newPath = [documentPath.path stringByAppendingPathComponent:@Demo/Wenchen]; if ([fileManager fileExistsAtPath:newPath] == false) { [fileManager createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil]; }
Then, open the sandbox and you will see that it is successfully created.
Create a file
Use Functions
The attributes here is nil unless you want to set some read/write permissions
- createFileAtPath:contents:attributes:
Return Bool to indicate whether the operation is successful. If an error occurs, the error message is in error.
The
NSFileManager * fileManager = [NSFileManager defaultManager]; NSArray * searchResult = [fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]; NSURL * documentPath = [searchResult firstObject]; NSString * newPath = [documentPath.path stringByAppendingPathComponent:@Demo/Wenchen]; if ([fileManager fileExistsAtPath:newPath] == false) { [fileManager createDirectoryAtPath:newPath withIntermediateDirectories:YES attributes:nil error:nil]; } NSString * filePath = [newPath stringByAppendingPathComponent:@file.txt]; NSData *data = [NSKeyedArchiver archivedDataWithRootObject:@blog.csdn.net/hello_hwc]; [fileManager createFileAtPath:filePath contents:data attributes:nil];
Check the sandbox and confirm the creation is successful.
Note: When Using writeToFile, if the file does not exist, it is automatically created.
Copy/move an object
Use Functions
- copyItemAtURL:toURL:error:- copyItemAtPath:toPath:error:- moveItemAtURL:toURL:error:- moveItemAtPath:toPath:error:
Return Bool to indicate whether the operation is successful. If an error occurs, the error message is in error.
Example
NSFileManager * fileManager = [NSFileManager defaultManager]; NSURL * libraryPath = [[fileManager URLsForDirectory:NSLibraryDirectory inDomains:NSUserDomainMask]firstObject]; NSURL * documentPath = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]firstObject]; NSString * oldPath = [libraryPath.path stringByAppendingPathComponent:@Demo/Wenchen/file.txt]; NSString * newPath = [documentPath.path stringByAppendingPathComponent:@file.txt]; [fileManager copyItemAtPath:oldPath toPath:newPath error:nil];
Check sandbox. Copied successfully
Delete
NSFileManager * fileManager = [NSFileManager defaultManager]; NSURL * documentPath = [[fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask]firstObject]; NSString * newPath = [documentPath.path stringByAppendingPathComponent:@file.txt]; Bool success = [fileManager removeItemAtPath:newPath error:nil];