我正在尝试从一个文件中获取内容并将其写入另一个文件.我读得很好,但我无法将其写入另一个文件.
I am trying to take content from one file and write it into another. I am reading fine, but I am not able to write it into another file.
我有一个单词数据库.我想根据字母数将单词分成不同的文件.所有四个字母的单词都放在一个文件中,依此类推.我在我的资源中添加了一个名为4letter"的 txt 文件,以下是我的代码:
I have a database of words. I want to separate the words into different files based on the number of letters. All four letter words go into one file, and so on. I added a txt file called "4letter" into my resources and the following is my code:
NSError *error; //READ NSString *dbFile = [[NSBundle mainBundle] pathForResource:@"words" ofType:@"txt"]; NSString *test = [NSString stringWithContentsOfFile:dbFile encoding:NSUTF8StringEncoding error:&error]; //convert from string to array NSArray *lines = [test componentsSeparatedByString:@" "]; NSFileHandle *logFile = nil; logFile = [NSFileHandle fileHandleForWritingAtPath:[[NSBundle mainBundle] pathForResource:@"4letter" ofType:@"txt"]]; //Test if write works for (int i=0; i<5; i++) { NSString *randomAnagram = [[lines objectAtIndex:i] lowercaseString]; [logFile writeData: [randomAnagram dataUsingEncoding: NSNEXTSTEPStringEncoding]]; } 推荐答案在 iOS 中,您不能写入应用程序包中的文件——整个包是只读的.改用进入 Documents 文件夹的路径.
In iOS, you can't write into a file in your app's bundle -- the entire bundle is read-only. Use a path into the Documents folder instead.
更多推荐
使用 [NSBundle mainBundle] 写入文件失败
发布评论