[英]Create new file and read it using NSFileManager objective c
question: I want to write to a new file some data that I already have into a NSMutableData . 问题:我想写一个新文件,我已经有一些数据存入NSMutableData。
I was reading in several places about this but it's not clear at all. 我在几个地方读到这个但是根本不清楚。 I was coding the next and It does not crash but I don´t know how to manipulate the "new file" since I don't know how to read it.
我正在编写下一个并且它没有崩溃,但我不知道如何操纵“新文件”,因为我不知道如何阅读它。
NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);
NSString* documentsDirectory = [paths objectAtIndex:0];
NSString* dataPath = [documentsDirectory stringByAppendingPathComponent:@"NewDataFile.txt"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) {
[[NSFileManager defaultManager] createFileAtPath:dataPath contents:dataBuffer attributes:nil];
Can someone complement this code or explain how to handle new files? 有人可以补充此代码或解释如何处理新文件?
There are many ways to read a file. 有很多方法可以读取文件。 Use NSFileHandle to open the file and read from it, or use something like NSData's dataWithContentsOfFile: or dataWithContentsOfURL:.
使用NSFileHandle打开文件并从中读取,或使用类似NSData的dataWithContentsOfFile:或dataWithContentsOfURL:。
The next link explain very well how to handle a file, it helped me a lot. 下一个链接很好地解释了如何处理文件,它帮助了我很多。
http://www.ios-developer.net/iphone-ipad-programmer/development/file-saving-and-loading/using-the-document-directory-to-store-files http://www.ios-developer.net/iphone-ipad-programmer/development/file-saving-and-loading/using-the-document-directory-to-store-files
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.