NSFileManager *fileManager = [NSFileManagerdefaultManager];
//获取document路径,括号中属性为当前应用程序独享
NSArray *directoryPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [directoryPaths objectAtIndex:0];
//定义记录文件全名以及路径的字符串filePath
NSString *filePath = [documentDirectory stringByAppendingPathComponent:@"UserNameAndPassWord.txt"];
//查找文件,如果不存在,就创建一个文件
if (![fileManager fileExistsAtPath:filePath]) {
[fileManager createFileAtPath:filePath contents:nil attributes:nil];
}
如果想检视文件有没有生成,可以在视图上添加一个Label,加一行代码显示地址。
Label.text = documentDirectory;
//或者
Label.text = filePath;
方法执行后的效果图,地址可以参考一下。