我能找到这个文件在哪里?

时间:2016-01-19 03:01:54

标签: ios objective-c

我使用writeToFile创建了input.txt文件,但是在项目路径中没有此文件我不知道如何找到它。

NSString* str2 = [NSString stringWithUTF8String:cstr];
[str2 writeToFile:@"input.txt" atomically:YES encoding:NSUTF8StringEncoding error:nil];
NSString* str3 = [NSString stringWithContentsOfFile:@"input.txt" encoding:NSUTF8StringEncoding error:nil];

1 个答案:

答案 0 :(得分:3)

您需要将app Documents文件夹的路径添加到您的文件中:

//get path to Documents 
NSString *documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//add path to you file name
NSString *fileWithPath = [documentPath stringByAppendingPathComponent:@"input.txt"];
//then write
[str2 writeToFile:fileWithPath atomically:TRUE encoding:NSUTF8StringEncoding error:NULL];

然后你也可以使用fileWithPath读取它:

NSString* str3 = [NSString stringWithContentsOfFile:fileWithPath encoding:NSUTF8StringEncoding error:nil];