1, get the program's home directory
[CPP]View Plaincopy
- NSString *homedirectory = Nshomedirectory ();
- NSLog (@"path:%@", homedirectory);
Printing results:
[CPP]View Plaincopy
- 2012-06-17 14:00:06.098 iossandbox[3536:f803]/users/rongfzh/library/application Support/iPhone SIMULATOR/5.1/ Applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2
What about the catalogue on the real machine? Let's see.
2012-06-17 14:25:47.059 iossandbox[4281:f803]/var/mobile/applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2
It can be seen that the directory on the real machine is/var/mobile/applications/this directory, and the emulator is not the same. This is the home directory, and the other subdirectories are the same as the emulator.
2. Get the document directory
[CPP]View Plaincopy
- Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
- NSString *path = [Paths objectatindex:0];
- NSLog (@"path:%@", Path);
Print results
[CPP]View Plaincopy
- 2012-06-17 14:00:06.099 iossandbox[3536:f803] path:/users/rongfzh/library/application Support/iPhone SIMULATOR/5.1/ Applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2/documents
3. Get the Cache directory
[CPP]View Plaincopy
- Nsarray *paths = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES);
- NSString *path = [Paths objectatindex:0];
- NSLog (@"%@", Path);
Print results
[CPP]View Plaincopy
- 2012-06-17 14:03:50.431 iossandbox[3628:f803]/users/rongfzh/library/application Support/iPhone SIMULATOR/5.1/ Applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2/library/caches
4. Get the Library directory
[CPP]View Plaincopy
- Nsarray *paths = Nssearchpathfordirectoriesindomains (Nslibrarydirectory, Nsuserdomainmask, YES);
- NSString *path = [Paths objectatindex:0];
- NSLog (@"%@", Path);
Print results
[CPP]View Plaincopy
- 2012-06-17 14:07:17.544 iossandbox[3733:f803]/users/rongfzh/library/application Support/iPhone SIMULATOR/5.1/ Applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2/library
5. Get the TMP directory
[CPP]View Plaincopy
- NSString *tmpdir = Nstemporarydirectory ();
- NSLog (@"%@", TmpDir);
Print results
[CPP]View Plaincopy
- 2012-06-17 14:08:07.824 iossandbox[3782:f803]/var/folders/g7/246bh79130zblw0yjjtc55cw0000gn/t/
6. Writing files
[CPP]View Plaincopy
- Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
- NSString *docdir = [Paths objectatindex:0];
- if (!docdir) {
- NSLog (@"Documents directory not Found");
- }
- Nsarray *array = [[Nsarray alloc] initwithobjects:@"Contents", @"content", nil];
- NSString *filepath = [Docdir stringbyappendingpathcomponent:@"TestFile.txt"];
- [Array Writetofile:filepath atomically:yes];
Note: We also run on the real machine, write the file, and then read the content from the real machine.
Write input array, inside is two strings, one will read out to print.
Write us in the program sandbox directory to see the file TestFile.txt
Open the file see the content is this, is an XML format of the plist file, the data format to save the content.
[CPP]View Plaincopy
- <?xml version="1.0" encoding="UTF-8"?>
- <! DOCTYPE plist public "-//apple//dtd plist 1.0//en" "Http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
- <plist version="1.0" >
- <array>
- <string> content </string>
- <string>content</string>
- </array>
- </plist>
7. Read the file
[CPP]View Plaincopy
- Nsarray *paths = Nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES);
- NSString *docdir = [Paths objectatindex:0];
- NSString *filepath = [Docdir stringbyappendingpathcomponent:@"TestFile.txt"];
- Nsarray *array = [[Nsarray Alloc]initwithcontentsoffile:filepath];
- NSLog (@"%@", array);
Printing results:
After parsing the above file, the contents are printed out.
[CPP]View Plaincopy
- 2012-06-17 14:14:46.249 iossandbox[3918:f803] (
- "\u5185\u5bb9",
- Content
- )
Read and print the file path on the real machine:
2012-06-17 14:25:47.059 iossandbox[4281:f803]/var/mobile/applications/3b8ec78a-5eee-4c2f-b0cb-4c3f02b996d2/ Documents/testfile.txt
(
"\u5185\u5bb9",
Content
)
can also be written and printed on the real machine.