THIS CONTENT DOWNLOAD SHORTLY

Objective

Main objective of this post is to give you an idea about File management in iOS

 

Introduction

If you are looking for an ultimate iOS file management tips then you can look here. NSFile Manager is an easy way to manage files. It has many attractive key concepts of the iOS file management. In this blog you can easily deal with both files and directories from iPhone application by covering all key concepts of file management (e.g. create file, read file, remove file etc). A short introduction of the NSFileManager class is necessary to learn; which is backbone of file management in iOS.

You will get Final Output: 

file management

 

Step 1 NSFileManager Class

It is used to perform many generic file-system operations. It supports both NSURL and NSString classes to specify the location of the file or directory. Some of its useful methods are as described below:

  1. (NSFileManager*)defaultManager: This method returns the shared file manager object for process.
  2. contentsOfDirectoryAtPath:error: : This method returns an object of NSArray containing the paths of items in the specified directory.
  3. createFileAtPath:contents:attributes: : It creates the file with specified content and attributes at the given location.
  4. removeItemAtPath:error: : It removes the file or directory from specified location.
  5. copyItemAtPath:toPath:error: :It copies the item from the specified path to the new location synchronously.
  6. moveItemAtPath:toPath:error: :It moves the file or directory at the specified location to the new location synchronously.
  7. fileExistsAtPath: (NSString *)path :It returns a boolean indicating weather a file or directory exist at specified path.

Now let’s have a look on the key concepts of the file management.

 

Step 2 Create file at specific path

NSFileManager *fileManager;
NSString *filePath;
NSString *docsDir;
NSArray *dirPaths;
fileManager = [NSFileManagerdefaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPathsobjectAtIndex:0];
filePath = [docsDirstringByAppendingPathComponent: @"datafile.txt"];
NSData *fileData = [@"File Manager Test"dataUsingEncoding:NSUTF8StringEncoding];
[fileManagercreateFileAtPath:filePathcontents:fileDataattributes:nil];
 

Step 3 Create directory at specific path

NSFileManager *fileManager;
NSString *docsDir;
NSArray *dirPaths;
fileManager = [NSFileManagerdefaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPathsobjectAtIndex:0];
NSString *folderPath = [docsDirstringByAppendingPathComponent:@"folder"];
[fileManagercreateDirectoryAtPath:folderPathwithIntermediateDirectories:NOattributes:nilerror:nil];
 

Step 4 Check if file exist at specificpath

NSFileManager *fileManager;
NSString *filePath;
NSString *docsDir;
NSArray *dirPaths;
fileManager = [NSFileManagerdefaultManager];
dirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
docsDir = [dirPathsobjectAtIndex:0];
filePath = [docsDirstringByAppendingPathComponent: @"datafile.txt"];
[fileManagerfileExistsAtPath:filePath];
 

Step 5 Check file content equality

filePath = [docsDirstringByAppendingPathComponent: @"datafile.txt"];
NSData *fileData = [@"File Manager Test"dataUsingEncoding:NSUTF8StringEncoding];
[fileManagercreateFileAtPath:filePathcontents:fileDataattributes:nil];
 
NSString *filePath2 = [docsDirstringByAppendingPathComponent:@"datafile2.txt"];
NSData *fileData2 = [@"File Manager"dataUsingEncoding:NSUTF8StringEncoding];
[fileManagercreateFileAtPath:filePath2 contents:fileData2 attributes:nil];
 
if ([fileManagercontentsEqualAtPath:filePathandPath:filePath2]) {
NSLog(@"Equal");
} else {
NSLog(@"Not Equal");
}
 

Step 6 Move File

NSString *oldFilePath = [docsDirstringByAppendingPathComponent: @"datafile.txt"];
NSString *newFilePath = [docsDirstringByAppendingPathComponent:@"test.txt"];
[fileManagermoveItemAtPath:oldFilePathtoPath:newFilePatherror:nil];
 

Step 7 Copy File

NSString *originalFile = [docsDirstringByAppendingPathComponent: @"original.txt"];
NSString *copyFile = [docsDirstringByAppendingPathComponent:@"copy.txt"];
[fileManagercopyItemAtPath:originalFiletoPath:copyFileerror:nil];
 

Step 8 Read File

filePath = [docsDirstringByAppendingPathComponent: @"datafile.txt"];
NSData *fileContent = [fileManagercontentsAtPath:filePath];
NSLog(@"%@",[[NSStringalloc] initWithData:fileContentencoding:NSUTF8StringEncoding]);
 

Step 9 Check file permissions

filePath = [docsDirstringByAppendingPathComponent: @"datafile.txt"];
if([fileManagerisWritableFileAtPath:filePath]) {
NSLog(@"file have write permission");
}
 
if ([fileManagerisReadableFileAtPath:filePath]) {
NSLog(@"file have read permission");
}
 
if ([fileManagerisExecutableFileAtPath:filePath]) {
NSLog(@"file have execute permission");
}
 

Step 10 Remove File

filePath = [docsDirstringByAppendingPathComponent: @"datafile.txt"];
[fileManagerremoveItemAtPath:filePatherror:nil];
 

Step 11 Get all content of specific directory

NSArray *arrDirectoryContent = [fileManagercontentsOfDirectoryAtPath:docsDirerror:nil];
NSLog(@"%@",arrDirectoryContent);

If you have got any query related to Ultimate iOS File Management Tips in comment them below.

Got an Idea of iPhone App Development? What are you still waiting for? Contact us now and see the Idea live soon. Our company has been named as one of the best iPhone App Development Company in India.

I am iOS Developer. I like to learn new technologies. I believe that any fool can write code that a computer can understand but good programmers write code that humans can understand.

face mask Belial The Demon Headgear Pocket Staff Magic