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!