咱們看看如何獲取應用程序沙盒目錄。包括真機的沙盒的目錄。 app
一、獲取程序的Home目錄 學習
- NSString *homeDirectory = NSHomeDirectory();
- NSLog(@"path:%@", homeDirectory);
打印結果:
- 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
那在真機上的目錄有是怎麼樣的呢?咱們看看 atom
2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2 spa
可見,真機上的目錄是/var/mobile/Applications/這個目錄下的,和模擬器不同。這個是Home目錄,其餘的子目錄和模擬器同樣。 .net
二、獲取document目錄
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *path = [paths objectAtIndex:0];
- NSLog(@"path:%@", path);
打印結果
- 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
三、獲取Cache目錄
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
- NSString *path = [paths objectAtIndex:0];
- NSLog(@"%@", path);
打印結果
- 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
四、獲取Library目錄
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
- NSString *path = [paths objectAtIndex:0];
- NSLog(@"%@", path);
打印結果
- 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、獲取Tmp目錄
- NSString *tmpDir = NSTemporaryDirectory();
- NSLog(@"%@", tmpDir);
打印結果
- 2012-06-17 14:08:07.824 IosSandbox[3782:f803] /var/folders/g7/246bh79130zblw0yjjtc55cw0000gn/T/
六、寫入文件
- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *docDir = [paths objectAtIndex:0];
- if (!docDir) {
- NSLog(@"Documents 目錄未找到");
- }
- NSArray *array = [[NSArray alloc] initWithObjects:@"內容",@"content",nil];
- NSString *filePath = [docDir stringByAppendingPathComponent:@"testFile.txt"];
- [array writeToFile:filePath atomically:YES];
注:咱們在真機上也運行一下,把文件寫入,下一步從真機上把內容讀取出來。 xml
寫入輸入 array ,裏面是兩個字符串,一會咱們讀出來打印。 blog
寫入咱們在程序沙盒目錄下看到文件 testFile.txt
打開文件看到的內容是這樣的,是個xml格式的plist文件,數據格式保存了內容。 ip
- <?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>內容</string>
- <string>content</string>
- </array>
- </plist>
七、讀取文件
- 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);
打印結果:
把上面的文件解析後,把內容打印出來了。 字符串
- 2012-06-17 14:14:46.249 IosSandbox[3918:f803] (
- "\U5185\U5bb9",
- content
- )
真機上讀取並打印文件路徑: get
2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Documents/testFile.txt
(
"\U5185\U5bb9",
content
)
真機上也能寫入和打印