接上篇
iOS學習之iOS沙盒(sandbox)機制和檔案操作(一)
我們看看如何擷取應用程式沙盒目錄。包括真機的沙盒的目錄。
1、擷取程式的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
那在真機上的目錄有是怎麼樣的呢?我們看看
2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2
可見,真機上的目錄是/var/mobile/Applications/這個目錄下的,和模拟器不一樣。這個是Home目錄,其他的子目錄和模拟器一樣。
2、擷取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
3、擷取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
4、擷取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/
6、寫入檔案
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];
注:我們在真機上也運作一下,把檔案寫入,下一步從真機上把内容讀取出來。
寫入輸入 array ,裡面是兩個字元串,一會我們讀出來列印。
寫入我們在程式沙盒目錄下看到檔案 testFile.txt

打開檔案看到的内容是這樣的,是個xml格式的plist檔案,資料格式儲存了内容。
<?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>
7、讀取檔案
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
)
真機上讀取并列印檔案路徑:
2012-06-17 14:25:47.059 IosSandbox[4281:f803] /var/mobile/Applications/3B8EC78A-5EEE-4C2F-B0CB-4C3F02B996D2/Documents/testFile.txt
(
"\U5185\U5bb9",
content
)
真機上也能寫入和列印。
著作權聲明:本文由http://blog.csdn.net/totogo2010/原創,歡迎轉載分享。請尊重作者勞動,轉載時保留該聲明和作者部落格連結,謝謝