打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
(1)NSFileManager的简单使用

 // NSFileManager : 文件管理类

// 使用defaultManager 类方法,创建出单例对象


// 1. 创建对象

NSFileManager *fileManager= [NSFileManager defaultManager];



// 2.创建文件夹,将要创建的/path1/path2文件夹拼接到路径中

NSString*newPath1 = [documentPath stringByAppendingPathComponent:@"path1"];

[fileManager createDirectoryAtPath:newPath1//路径,包含要创建的新文件夹

     withIntermediateDirectories:YES //设置是否可以同时创建多级目录下的文件夹

                    attributes:nil

                        error:nil];



// 3. 创建文件

NSString*filePath1= [newPath1 stringByAppendingPathComponent:@"test.txt"];

BOOLres = [fileManager createFileAtPath:filePath1contents:nilattributes:nil];

if(res) {

   NSLog(@"文件创建成功");

} else{

   NSLog(@"文件创建失败");

}



// 4. 写入文件

NSString*strData = @"这是准备写入的数据";

BOOLres2= [strData writeToFile:filePath1atomically:YESencoding:NSUTF8StringEncodingerror:nil];

if(res2) {

   NSLog(@"文件写入成功");

} else{

   NSLog(@"文件写入失败");

}

NSLog(@"%@",filePath1);




// 5.判断文件是否存在

if([fileManager fileExistsAtPath:filePath1]){

    NSLog(@"文件存在");

} else{

   NSLog(@"文件不存在");

}



// 6. 删除文件

BOOLres3 = [fileManager removeItemAtPath:filePath1error:nil];

if(res3) {

   NSLog(@"文件删除成功");

} else{

   NSLog(@"文件删除失败");

}


本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
iPhone文件系统NSFileManager讲解
iOS开发
[ios] UIWebView的离线缓存【转】
iosNSFileManager用法详解(转)
[iOS翻译]《iOS 7 Programming Cookbook》:iOS文件与文件夹管理(上)
【iOS】线程安全的文件读写
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服