IOS 文件读写之:创建文件


废话少讲,线上在实现文件的方法里面的主要代码,主要分四个步骤:

 

    //创建文件管理器

    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;

 

方法执行后的效果图,地址可以参考一下。

   


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM