[Objective-c] 013_ filesystem (file system)

Source: Internet
Author: User
Tags tmp folder

In the previous three articles about data persistence, we all use files (plist files, database files) that are stored in the document directory. The file mechanism of iOS is a sandbox mechanism where apps can only access files in their own app directory. Content generated by iOS apps, like, files, cached content, and so on, must be stored in their own sandbox. By default, each sandbox contains 3 folders: Documents, Library, and TMP. The library contains caches, preferences directories.

Documents: Apple recommends that the files generated by the program creation and the file data generated by the app's browsing be saved in this directory, which is included in itunes backup and restore
Library: The default settings or other status information of the stored program;
Library/caches: Store the cache file, save the persisted data of the application, use it to apply the upgrade or save the data after the app is closed, and not be synced by itunes, so in order to reduce the time of synchronization, consider putting some larger files into this directory without needing backup.
TMP: Provides an immediate place to create temporary files, but does not need to be persisted, after the application is closed, the data in the directory will be deleted, or the system may be cleared when the program is not running.

We actually how to come or take the sandbox, how to do the appropriate operation in the sandbox, the following one by one decomposition.

1. Get the app's sandbox root directory

NSString *approotdir=nshomedirectory (); NSLog (@ "My sandbox path root path------":%@ ", Approotdir);

2. get the Documents directory path

The first kind of nsstring *approotdir=nshomedirectory (); NSLog (@ "My sandbox path root path------":%@ ", Approotdir); NSString *documentdir = [Approotdir stringbyappendingpathcomponent:@ "Documents"]; NSLog (@ "Documentdir:-----"%@ ", Documentdir);    The second type of Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); NSString *documentsdirectory = [Paths objectatindex:0]; NSLog (@ "documentsdirectory:-----"%@ ", documentsdirectory);

3. get the library (including caches, Preferences) directory path:

The first kind of nsstring *approotdir=nshomedirectory (); NSLog (@ "My sandbox path root path------":%@ ", Approotdir); NSString *documentdir = [Approotdir stringbyappendingpathcomponent:@ "Library"]; NSLog (@ "Documentdir:-----"%@ ", documentdir);//The second Nsarray *paths = Nssearchpathfordirectoriesindomains ( Nslibrarydirectory, Nsuserdomainmask, YES); NSString *librarydirectory = [Paths objectatindex:0]; NSLog (@ "librarydirectory:-----"%@ ", librarydirectory);//cachesnsstring *cachespath = [librarydirectory stringbyappendingstring:@ "Caches"];//preferencesnsstring *preferencespath = [librarydirectory stringbyappendingstring:@ "Preferences"]; NSLog (@ "Cachespath:-----"%@ ", Cachespath); NSLog (@ "Preferencespath:-----"%@ ", Preferencespath);

4. Get the TMP directory path:

The first kind of nsstring *approotdir=nshomedirectory (); NSLog (@ "My sandbox path root path------":%@ ", Approotdir); NSString *documentdir = [Approotdir stringbyappendingpathcomponent:@ "TMP"]; NSLog (@ "Documentdir:-----"%@ ", documentdir);//The second nsstring *tmpdirectory = Nstemporarydirectory (); NSLog (@ "tmpdirectory:-----"%@ ", tmpdirectory);

5. Create a file (in the TMP folder)

NSString *tmpdirectory = Nstemporarydirectory (); NSLog (@ "tmpdirectory:-----"%@ ", tmpdirectory); Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; NSString *testfilepath = [tmpdirectory stringbyappendingpathcomponent:@ "TestFile.txt"]; BOOL Res=[filemanager createfileatpath:testfilepath Contents:nil attributes:nil];if (res) {    NSLog (@ "Test file creation succeeded:%@" , Testfilepath);} else {    NSLog (@ "Test file creation Failed");}

6. Create a folder

Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; NSString *testfolderdirectory = [Documentspath stringbyappendingpathcomponent:@ "Test"];//Create directory bool Res=[fileManager Createdirectoryatpath:testfolderdirectory withintermediatedirectories:yes Attributes:nil error:nil];if (res) {    NSLog (@ "Test folder creation succeeded");} else {    NSLog (@ "Test folder creation failed");}

7. deleting files

NSString *tmpdirectory = Nstemporarydirectory (); Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager]; NSString *testfilepath = [tmpdirectory stringbyappendingpathcomponent:@ "TestFile.txt"];//determine if the file exists if ([FileManager Fileexistsatpath:testfilepath]) {    BOOL res=[filemanager removeitematpath:testfilepath Error:nil];    if (res) {        NSLog (@ "Testfile file deleted successfully");    } else        NSLog (@ "testfile file deletion failed");}

8. Writing files

NSString *tmpdirectory = Nstemporarydirectory (); NSLog (@ "tmpdirectory:-----"%@ ", tmpdirectory); NSString *[email protected] "www.babybus.com Superdo"; NSString *testfilepath = [tmpdirectory stringbyappendingpathcomponent:@ "TestFile.txt"]; BOOL res=[content writetofile:testfilepath atomically:yes encoding:nsutf8stringencoding error:nil];if (res) {    NSLog (@ "testfile file successfully written");} else {    NSLog (@ "Testfile file write Failed");}

These are some simple and common operations for iOS files. For more details please refer to (https://developer.apple.com/library/ios/#documentation/filemanagement/conceptual/ FILESYSTEMPROGRAMMINGGUIDE/FILESYSTEMOVERVIEW/FILESYSTEMOVERVIEW.HTML#//APPLE_REF/DOC/UID/TP40010672-CH2-SW2)

This site article is for baby bus SD. Team Original, reproduced must be clearly noted: (the author's official website: Baby bus )
Reprinted from "Baby bus Superdo Team" original link: http://www.cnblogs.com/superdo/p/4659923.html

[Objective-c] 013_ filesystem (file system)

Related Article

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.