每個應用程式裡都有一個 Document 資料夾,內容通常存放一些動態的使用者資料,與一般暫存區不同的是 Document 資料夾中的檔案在結束應用程式之後,仍然會保留,因此你可以將一些應用程式的環境設定參數存入其中,以便在下一次執行應用程式時做環境上的設定。下面將以 plist(Property List)檔案為例,實作讀取、寫入、清除等動作。
讀取檔案
在讀取檔案的部份,首先要取得在 Document 中的檔案路徑,之後我們利用該路徑來判斷檔案是否存在,才予以讀取。
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
 | 
 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingString:@"/Data.plist"]; 
 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
 
 
if ([fileManager fileExistsAtPath: filePath]) { 
    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 
 
    [textView setText:[NSString stringWithFormat:@"%@", data]]; 
} else{  
    [textView setText:@"沒有資料,讀取失敗!"]; 
} 
 
 | 
 
 
寫入檔案
在寫入檔案的部份,主要分成設定資料與覆寫兩部分,它門主要的差異所要操作的檔案是否已經存在於 Document 中,如果檔案已經存在就直接讀取檔案資料來做修改,否則就建立新的檔案。
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
 | 
 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingString:@"/Data.plist"]; 
 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
NSMutableDictionary *data; 
 
 
if ([fileManager fileExistsAtPath: filePath]) { 
 
     
    data = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath]; 
 
} else { 
 
     
    data = [[NSMutableDictionary alloc] init];            
} 
 
 
[data setValue:[NSString stringWithFormat:@"%0.f", [hp_Slider value]] forKey:@"HP"]; 
[data setValue:[NSString stringWithFormat:@"%0.f", [mp_Slider value]] forKey:@"MP"]; 
[data setValue:[NSString stringWithFormat:@"%0.f", [atk_Slider value]] forKey:@"ATK"]; 
[data setValue:[NSString stringWithFormat:@"%0.f", [def_Slider value]] forKey:@"DEF"]; 
 
 
if ([data writeToFile:filePath atomically: YES]) { 
    [textView setText:@"資料寫入成功!"]; 
} else { 
    [textView setText:@"資料寫入失敗!"]; 
} 
 
 | 
 
 
清除檔案
在清除檔案的部份,同樣先判斷檔案是否存在於 Document 中,才予以刪除。
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
 | 
 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *filePath = [documentsDirectory stringByAppendingString:@"/Data.plist"]; 
 
NSFileManager *fileManager = [NSFileManager defaultManager]; 
 
 
if ([fileManager fileExistsAtPath: filePath]) { 
    [fileManager removeItemAtPath:filePath error:NULL]; 
 
    [textView setText:@"資料清除成功!"]; 
} 
 
 | 
 
 
其他的資料存放路徑
在應用程式中還存在著其他的檔案存放路徑,像是常用到的專案內的靜態檔案路徑 pathForResource 或是資料暫存區 NSTemporaryDirectory() 等下面就列出一些常用的檔案存放路徑。
1 
2 
3 
4 
5 
6 
7 
8 
 | 
 
NSString *path = [[NSBundle mainBundle] pathForResource:@"檔名" ofType:@"副檔名"]; 
 
 
NSString *path = NSHomeDirectory(); 
 
 
NSString *path = NSTemporaryDirectory(); 
 
 | 
 
 
最後,如果想要更有系統的存取大筆資料,可以考慮使用內建的輕量資料庫 SQLite3,關於 SQLite3 的基本使用方式可以參考 
SQLite3 的取值方式一文。
 
沒有留言:
張貼留言