iOS中NSFileManager文件常用操作整合
//获取Document路径
+ (NSString *)getDocumentPath
{
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return [filePaths objectAtIndex:];
} //获取Library路径
+ (NSString *)getLibraryPath
{
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
return [filePaths objectAtIndex:];
} //获取应用程序路径
+ (NSString *)getApplicationPath
{
return NSHomeDirectory();
} //获取Cache路径
+ (NSString *)getCachePath
{
NSArray *filePaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
return [filePaths objectAtIndex:];
} //获取Temp路径
+ (NSString *)getTempPath
{
return NSTemporaryDirectory();
} //判断文件是否存在于某个路径中
+ (BOOL)fileIsExistOfPath:(NSString *)filePath
{
BOOL flag = NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {
flag = YES;
} else {
flag = NO;
}
return flag;
} //从某个路径中移除文件
+ (BOOL)removeFileOfPath:(NSString *)filePath
{
BOOL flag = YES;
NSFileManager *fileManage = [NSFileManager defaultManager];
if ([fileManage fileExistsAtPath:filePath]) {
if (![fileManage removeItemAtPath:filePath error:nil]) {
flag = NO;
}
}
return flag;
} //从URL路径中移除文件
- (BOOL)removeFileOfURL:(NSURL *)fileURL
{
BOOL flag = YES;
NSFileManager *fileManage = [NSFileManager defaultManager];
if ([fileManage fileExistsAtPath:fileURL.path]) {
if (![fileManage removeItemAtURL:fileURL error:nil]) {
flag = NO;
}
}
return flag;
} //创建文件路径
+(BOOL)creatDirectoryWithPath:(NSString *)dirPath
{
BOOL ret = YES;
BOOL isExist = [[NSFileManager defaultManager] fileExistsAtPath:dirPath];
if (!isExist) {
NSError *error;
BOOL isSuccess = [[NSFileManager defaultManager] createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
if (!isSuccess) {
ret = NO;
NSLog(@"creat Directory Failed. errorInfo:%@",error);
}
}
return ret;
} //创建文件
+ (BOOL)creatFileWithPath:(NSString *)filePath
{
BOOL isSuccess = YES;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL temp = [fileManager fileExistsAtPath:filePath];
if (temp) {
return YES;
}
NSError *error;
//stringByDeletingLastPathComponent:删除最后一个路径节点
NSString *dirPath = [filePath stringByDeletingLastPathComponent];
isSuccess = [fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error];
if (error) {
NSLog(@"creat File Failed. errorInfo:%@",error);
}
if (!isSuccess) {
return isSuccess;
}
isSuccess = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
return isSuccess;
} //保存文件
+ (BOOL)saveFile:(NSString *)filePath withData:(NSData *)data
{
BOOL ret = YES;
ret = [self creatFileWithPath:filePath];
if (ret) {
ret = [data writeToFile:filePath atomically:YES];
if (!ret) {
NSLog(@"%s Failed",__FUNCTION__);
}
} else {
NSLog(@"%s Failed",__FUNCTION__);
}
return ret;
} //追加写文件
+ (BOOL)appendData:(NSData *)data withPath:(NSString *)path
{
BOOL result = [self creatFileWithPath:path];
if (result) {
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:path];
[handle seekToEndOfFile];
[handle writeData:data];
[handle synchronizeFile];
[handle closeFile];
return YES;
} else {
NSLog(@"%s Failed",__FUNCTION__);
return NO;
}
} //获取文件
+ (NSData *)getFileData:(NSString *)filePath
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *fileData = [handle readDataToEndOfFile];
[handle closeFile];
return fileData;
} //读取文件
+ (NSData *)getFileData:(NSString *)filePath startIndex:(long long)startIndex length:(NSInteger)length
{
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:filePath];
[handle seekToFileOffset:startIndex];
NSData *data = [handle readDataOfLength:length];
[handle closeFile];
return data;
} //移动文件
+ (BOOL)moveFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:fromPath]) {
NSLog(@"Error: fromPath Not Exist");
return NO;
}
if (![fileManager fileExistsAtPath:toPath]) {
NSLog(@"Error: toPath Not Exist");
return NO;
}
NSString *headerComponent = [toPath stringByDeletingLastPathComponent];
if ([self creatFileWithPath:headerComponent]) {
return [fileManager moveItemAtPath:fromPath toPath:toPath error:nil];
} else {
return NO;
}
} //拷贝文件
+(BOOL)copyFileFromPath:(NSString *)fromPath toPath:(NSString *)toPath
{
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:fromPath]) {
NSLog(@"Error: fromPath Not Exist");
return NO;
}
if (![fileManager fileExistsAtPath:toPath]) {
NSLog(@"Error: toPath Not Exist");
return NO;
}
NSString *headerComponent = [toPath stringByDeletingLastPathComponent];
if ([self creatFileWithPath:headerComponent]) {
return [fileManager copyItemAtPath:fromPath toPath:toPath error:nil];
} else {
return NO;
}
} //获取文件夹下文件列表
+ (NSArray *)getFileListInFolderWithPath:(NSString *)path
{
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *fileList = [fileManager contentsOfDirectoryAtPath:path error:&error];
if (error) {
NSLog(@"getFileListInFolderWithPathFailed, errorInfo:%@",error);
}
return fileList;
} //获取文件大小
+ (long long)getFileSizeWithPath:(NSString *)path
{
unsigned long long fileLength = ;
NSNumber *fileSize;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
if ((fileSize = [fileAttributes objectForKey:NSFileSize])) {
fileLength = [fileSize unsignedLongLongValue];
}
return fileLength; // NSFileManager* manager =[NSFileManager defaultManager];
// if ([manager fileExistsAtPath:path]){
// return [[manager attributesOfItemAtPath:path error:nil] fileSize];
// }
// return 0;
} //获取文件创建时间
+ (NSString *)getFileCreatDateWithPath:(NSString *)path
{
NSString *date = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
date = [fileAttributes objectForKey:NSFileCreationDate];
return date;
} //获取文件所有者
+ (NSString *)getFileOwnerWithPath:(NSString *)path
{
NSString *fileOwner = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
fileOwner = [fileAttributes objectForKey:NSFileOwnerAccountName];
return fileOwner;
} //获取文件更改日期
+ (NSString *)getFileChangeDateWithPath:(NSString *)path
{
NSString *date = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath:path error:nil];
date = [fileAttributes objectForKey:NSFileModificationDate];
return date;
}
iOS中NSFileManager文件常用操作整合的更多相关文章
- Hadoop HDFS文件常用操作及注意事项
Hadoop HDFS文件常用操作及注意事项 1.Copy a file from the local file system to HDFS The srcFile variable needs t ...
- go语言之进阶篇文件常用操作接口介绍和使用
一.文件常用操作接口介绍 1.创建文件 法1: 推荐用法 func Create(name string) (file *File, err Error) 根据提供的文件名创建新的文件,返回一个文件对 ...
- python 异常处理、文件常用操作
异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm
- OC NSFileManager(文件路径操作)
OC NSFileManager(文件路径操作) 初始化 NSFileManager * fm = [NSFileManager defaultManager]; 获取当前目录 [fm current ...
- Python基础灬文件常用操作
文件常用操作 文件内建函数和方法 open() :打开文件 read():输入 readline():输入一行 seek():文件内移动 write():输出 close():关闭文件 写文件writ ...
- iOS中几种常用的数据存储方式
自己稍微总结了一下下,方便大家查看 1.write直接写入文件的方法 永久保存在磁盘中,可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...
- java中 File文件常用操作方法的汇总
一.IO流: 1.全称为:Input Output---------输入输出流. 输入:将文件读到内存中. 输出:将文件从内存中输出到其他地方. 2.IO技术的作用: 主要是解决设备与设备之间的数据传 ...
- linux下拷贝命令中的文件过滤操作记录
在日常的运维工作中,经常会涉及到在拷贝某个目录时要排查其中的某些文件.废话不多说,下面对这一需求的操作做一记录: linux系统中,假设要想将目录A中的文件复制到目录B中,并且复制时过滤掉源目录A中的 ...
- [转]Windows系统中监控文件复制操作的几种方式
1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得到源和目标文件名. 可以控制拒绝操作. 缺点: 不能对文件进行控制. 只对Shell文件操作有效, 对原生 ...
随机推荐
- server 2012系统更改电脑密码
在server2012系统中将鼠标指针移至任务栏右侧,在弹出的操作栏中单击“设置”选项. 在打开的设置操作界面中,鼠标单击“控制面板”选项. 在打开的控制面板选项窗口中,单击“管理工具”选 ...
- Debian初识(选择最佳镜像发布站点加入source.list文件)
选择最佳镜像发布站点加入source.list文件:netselect,netselect-apt “该将哪个Debian镜像发布站点加入source.list文件?”.有很多方法来选择镜像发布站点, ...
- html基础1(环境准备、标签)
学习目的 1,能改前端的模板 2,自己装修页面 3.前后端交互多个技术 4.能操作网页元素 5.能和前端开发人员沟通 开发工具: pycharm/webStorm EditPlus(适合初学) sub ...
- php的闭包
闭包是指在创建时封装周围状态的函数,即使闭包所在的环境的不存在了,闭包中封装的状态依然存在. 匿名函数其实就是没有名称的函数,匿名函数可以赋值给变量,还能像其他任何PHP函数对象那样传递.不过匿名函数 ...
- cordova 安装使用
前人总结: Cordova是Apache软件基金会的一个产品.其前身是PhoneGap,由Nitobi开发,2011年10月,Adobe收够了Nitobi,并且PhoneGap项目也被贡献给Apach ...
- Java-Runoob-面向对象:Java 接口
ylbtech-Java-Runoob-面向对象:Java 接口 1.返回顶部 1. Java 接口 接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以 ...
- 第一章 先把Kubernetes跑起来
1.1 先跑起来 k8s官网已经为大家准备好了一个现成的最小可用系统. https://kubernetes.io/docs/tutorials/kubernetes-basics/ 1.2 创建K ...
- css3 box-shadow阴影(外阴影与外发光)讲解
基础说明: 外阴影:box-shadow: X轴 Y轴 Rpx color; 属性说明(顺序依次对应): 阴影的X轴(可以使用负值) 阴影的Y轴(可以使用负值) 阴影 ...
- socket编程之select()
int select(int maxfdp,fd_set *readfds,fd_set *writefds,fd_set *errorfds,struct timeval *timeout); 参数 ...
- 渗透辅助神器 - DZGEN
项目地址:https://github.com/joker25000/DZGEN git clone ┌─[root@sch01ar]─[/sch01ar] └──╼ #git clone https ...