iOS file Management-nsfilemangager-nsdata

Source: Internet
Author: User

File management under iOS, which does not have access to any file directories under iOS like the Windows file system, such as C-drives, D-drives, etc. In iOS, each application can only access the current program's directory, which is sandbox (sandbox model).

iOS assigns a directory to each application, which has a default of four file directories:

App: Our program develops compressed package files that contain the assembly and resource files inside.

Documents: Application data is stored in documents, except for the preferred settings on Nsuerdefaults

Library: Nsuserdefaults-based preferred settings are stored in the Library/perfercens folder

TMP: Stores temporary files.

My task in this development is to write the relevant error log and write it to a local file, so the path I chose is documents.

I use the Nsfilemanager class for file management, NSData for file reading, Nsmutabledata file writing scheme to implement.

The problem encountered in creating the Nsfilemanager class was not many, but when I read the file with NSData, I encountered a problem where I could never get the handle (and I couldn't get the content information)

At first I used the method: [NSData Datawithcontentsoffile:path] path is the full path to the entire file, but no matter how I try, I still can't get the contents.

Later I combined the Nsfilemanager to read the data (where the file is already there) the steps are as follows:

For example: The file we want to read is: Documents/cordova/log/201405.log

1, need to pass [Nsfilemanager Changecurrentdircectorypath:[filepath Stringbyexpandingtildeinpath]]; method to switch to the Documents/cordova/log directory below. (FilePath is the specified all the directories can refer to the Nssearchpathfordirectoriesindomains method)

2. Then use NSData *reader= [NSData datawithcontentsoffile:@ "201405.log"] method to directly read the file data under the current folder, where the data has been read into memory.

3, the last append content to write,

Nsmutabledata *write=[[nsmutabledata Alloc] Initwithdata:reader];--nsdata initialization

[Write Appenddata:meage]--Add data

[Write writetofile:@ "201405.log" atomically:yes];

In fact, I do not understand why directly to the NSData full path will be the problem of not getting data. Also heard many people say Nsurl way to write and read data better. The next time you have a good experience .... It's reserved here!

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.