IPhone file structure and file operations

Source: Internet
Author: User

This article focuses on file operations in the sandbox storage mode. The details are as follows:

For an app running on the iPhoneOnly accessSome files under your root directory (so-called sandbox-sandbox ).

After an app is published to the iPhone, its directory structure is as follows:

1. The app root can use Nshomedirectory ()Access to; 2. The documents directory is where we can write and save the file. Generally, we can get it through the following method:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentsDirectory = [paths objectAtIndex:0];  

3. In the tmp directory, we can write some data needed for running the program,The written data will not exist after the program exits.. You can use

Nsstring * nstemporarydirectory (void); Method;

4. file operations can be performed through nsfilemanage.[Nsfilemanger defaultmanger]Obtain the instance.

Related Operations:

A. Create a directory or file:

For exampleDocumentsCreate a test directory,

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);    NSString *documentsDirectory = [paths objectAtIndex:0];    NSLog(@”%@”,documentsDirectory);    NSFileManager *fileManage = [NSFileManager defaultManager];    NSString *myDirectory = [documentsDirectory stringByAppendingPathComponent:@“test”];    BOOL ok = [fileManage createDirectoryAtPath:myDirectory attributes:nil];  

To create a file.txt file under documents:

// The result is/documents/file.txt nsstring * filepath = [documentsdirectory stringbyappendingpathcomponent: @ "file.txt"];

B. Get all file names in a directory:

 

// Above mydirectory) Available nsarray * file = [filemanager subpathsofdirectoryatpath: mydirectory error: Nil]; or nsarray * files = [filemanager subpathsatpath: mydirectory];

C. Read an object:

 

Nsdata * Data = [filemanger contentsatpath: myfilepath]; // myfilepath is the file name that contains the complete path or the class method that uses nsdata directly: nsdata * Data = [nsdata datawithcontentofpath: myfilepath];

D. save an object:

// You can use nsfilemanager in the following methods:-(bool) createfileatpath :( nsstring *) path contents :( nsdata *) data attributes :( nsdictionary *) ATTR; or nsdata-(bool) writetofile :( nsstring *) path atomically :( bool) useauxiliaryfile;-(bool) writetofile :( nsstring *) Path Options :( nsuinteger) writeoptionsmask error :( nserror **) errorptr;

E. delete an object:

// You can use nsfilemanager in the following ways: // removes the file or directory at the specified path. -(bool) removeitematpath :( nsstring *) path error :( nserror **) Error // removes the file or directory at the specified URL. -(bool) removeitematurl :( nsurl *) URL error :( nserror **) Error

F. Move or rename a file

// To rename an object, we need to move the object to a new path. The following code creates the desired path of the target file, and then requests to move the file and display the file directory after moving it. // Rename nsstring * filepath2 = [documentsdirectory stringbyappendingpathcomponent: @ "file2.txt"] by moving the file; // determine whether to move if ([filemanager moveitematpath: filepath topath: filepath2 error: & error]! = Yes) nslog (@ "unable to move file: % @", [error localizeddescription]); // displays the nslog (@ "documentsdirectory: % @", [filemanager contentsofdirectoryatpath: documentsdirectoryerror: & error]);

How to read and write data using the iPhone official SDK

We know that for security reasons, the official iPhone SDK cannot write files as freely as the toolchain does.

Note:Both methods are stored in/documents.

bool writeApplicationData(NSData *data, NSString *fileName)      {          NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);          NSString *documentsDirectory = [paths objectAtIndex:0];          if (!documentsDirectory) {              NSLog(@"Documents directory not found!");              return NO;             }          NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];         return ([data writeToFile:appFile atomically:YES]);     }                   NSData *applicationDataFromFile(NSString *fileName)     {        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        NSString *documentsDirectory = [paths objectAtIndex:0];        NSString *appFile = [documentsDirectory stringByAppendingPathComponent:fileName];        NSData *myData = [[[NSData alloc] initWithContentsOfFile:appFile] autorelease];        return myData;  }  

And this article from: http://blog.chinaunix.net/space.php? Uid = 20622737 & Do = Blog & id = 1912783, which has been re-edited and slightly modified.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.