首页 技术 正文
技术 2022年11月15日
0 收藏 699 点赞 2,316 浏览 2124 个字

#import “ViewController.h”

@implementation ViewController

– (void)viewDidLoad {

[super viewDidLoad];

[self handleNSFileManage];

}

// 文件管理

– (void)handleNSFileManage{

// NSFileManager 是一个单例类,我们称之为文件管理类,是一个专门用来管理文件的工具,主要可以完成以下功能:文件的添加,文件的删除,文件的移动,文件的拷贝;

// 创建文件管理对象

NSFileManager *fileManage = [NSFileManager defaultManager];

// 1.文件的添加

// 例如:要在Documents文件夹下创建一个File1文件夹

// ①首先要获取Documents文件夹路径

NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES)lastObject];

// ②接着需要准备要创建的文件路径

NSString *File1Path = [documentsPath stringByAppendingPathComponent:@”Flie1″];

// ③创建文件夹

// 参数1:文件路径

// 参数2:如果文件中已经有别的目录,是否还要创建

// 参数3,4:属性,报错信息,都给nil

// 用来判断要创建的文件是否存在

BOOL isHave = [fileManage fileExistsAtPath:File1Path];

if (isHave) {

NSLog(@”文件已存在”);

}else{

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

BOOL isSuccess = [fileManage createDirectoryAtPath:File1Path withIntermediateDirectories:YES attributes:nil error:nil];

NSLog(@”%@”,isSuccess ? @”创建成功” : @”创建失败”);

}

NSLog(@”%@”,File1Path);

// 2.文件的删除

// 判断要删除的文件是否存在

if ([fileManage fileExistsAtPath:File1Path]) {

NSLog(@”文件存在”);

// 删除

BOOL isSuccess = [fileManage removeItemAtPath:File1Path error:nil];

NSLog(@”%@”,isSuccess ? @”删除成功” : @”删除失败”);

}else{

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

}

// 3.文件的拷贝

//  简单示范:准备一个Love.txt文件拖入工程,拷贝到File1文件夹中

// ①获取要拷贝的文件路径

NSString *lovePath = [[NSBundle mainBundle]pathForResource:@”Love” ofType:txt];

// ②准备要拷贝过去的文件路径

NSString *toLovePath = [File1Path stringByAppendingPathComponent:@”Love.txt”];

// 简单判断拷贝过去的文件路径是否存在

if (![fileManage fileExistsAtPath:toLovePath]) {

BOOL isSuccess = [fileManage copyItemAtPath:lovePath toPath:toLovePath error:nil];

NSLog(@”%@”,isSuccess ? @”拷贝成功” : @”拷贝失败”);

}

// 4.文件的移动

// 例如:将file1文件移动到library文件夹下

// ①获取library文件路径

NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)lastObject];

// 获取在libraryPath文件中加入file1的路径

NSString *toFilePath = [libraryPath stringByAppendingPathComponent:@”File1″];

if (![fileManage fileExistsAtPath:toFilePath]) {

BOOL isSuccess = [fileManage moveItemAtPath:File1Path toPath:toFilePath error:nil];

NSLog(@”%@”,isSuccess ? @”移动成功” : @”移动失败”);

}

// 通过NSFileManage计算文件的大小

// 计算toLovePath路径下的文件大小

// NSDictionary *info = [fileManage attributesOfItemAtPath:toLovePath error:nil];

// NSLog(@”%lfM”,info.fileSize/1024.0/1024.0);

}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919