我正在製作一個OX Cocoa應用程序,我希望能夠使用按鈕按下應用程序來讀寫文本文件。這些文本文件應該保存在/ Library/Application Support/AppName中,但是我無法讓我的應用程序從那裏讀取任何內容。它可以寫入文件夾,但不會讀取它寫入的內容,即使我可以在查找器中看到文件。如何從/庫/應用程序支持/文件夾中讀取?
這裏是我使用的成功寫入到該文件夾中的代碼。
NSString *text = editor.string;
NSString *path = @"/Library/Application Support/";
NSMutableString *mu = [[NSMutableString stringWithString:path] init];
[mu insertString:FileName.stringValue atIndex:mu.length];
[mu insertString:@".txt" atIndex:mu.length];
path = [mu copy];
[text writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:NULL];
這是我使用(和失敗)從文本文件中讀取的代碼。
NSArray *path = [[NSBundle mainBundle] pathsForResourcesOfType:@"txt" inDirectory:@"/Library/Application Support/"];
NSString *output = @"";
NSMutableString *mu = [[NSMutableString stringWithString:output] init];
for (int i = 0; i < [path count]; i++) {
NSString *text = [NSString stringWithContentsOfFile:path[i] encoding:NSUTF8StringEncoding error:NULL];
[mu insertString:text atIndex:mu.length];
[mu insertString:@"\n" atIndex:mu.length];
}
[textView setString:mu];
我能糾正的任何提示都會超級有用,我有點卡在這裏。
編輯:使用您輸入我已經更新了我的代碼如下:
NSString *fileLocation = @"~/Library/Application Support/";
NSArray *text = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:fileLocation error:nil];
NSString *output = @"";
NSMutableString *mu = [[NSMutableString stringWithString:output] init];
for (int i = 0; i < [text count]; i++) {
[mu insertString:text[i] atIndex:mu.length];
[mu insertString:@"\n" atIndex:mu.length];
}
[textView setString:mu];
但是從文件的文本仍然沒有出現。
在您的原始代碼中編寫您指向/ Library/Application Support的文件。你有沒有改變它以匹配你在這裏使用的〜/ Library/Application Support? – 2013-02-14 22:07:30
是否有所作爲? – 2013-02-14 22:09:01
是的。 〜擴展爲/ Users/*用戶名*/Library/Application Support,不帶〜進入全局/庫/應用程序支持文件夾。 – 2013-02-14 22:09:52