//获取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文件常用操作整合的更多相关文章

  1. Hadoop HDFS文件常用操作及注意事项

    Hadoop HDFS文件常用操作及注意事项 1.Copy a file from the local file system to HDFS The srcFile variable needs t ...

  2. go语言之进阶篇文件常用操作接口介绍和使用

    一.文件常用操作接口介绍 1.创建文件 法1: 推荐用法 func Create(name string) (file *File, err Error) 根据提供的文件名创建新的文件,返回一个文件对 ...

  3. python 异常处理、文件常用操作

    异常处理 http://www.jb51.net/article/95033.htm 文件常用操作 http://www.jb51.net/article/92946.htm

  4. OC NSFileManager(文件路径操作)

    OC NSFileManager(文件路径操作) 初始化 NSFileManager * fm = [NSFileManager defaultManager]; 获取当前目录 [fm current ...

  5. Python基础灬文件常用操作

    文件常用操作 文件内建函数和方法 open() :打开文件 read():输入 readline():输入一行 seek():文件内移动 write():输出 close():关闭文件 写文件writ ...

  6. iOS中几种常用的数据存储方式

    自己稍微总结了一下下,方便大家查看 1.write直接写入文件的方法 永久保存在磁盘中,可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...

  7. java中 File文件常用操作方法的汇总

    一.IO流: 1.全称为:Input Output---------输入输出流. 输入:将文件读到内存中. 输出:将文件从内存中输出到其他地方. 2.IO技术的作用: 主要是解决设备与设备之间的数据传 ...

  8. linux下拷贝命令中的文件过滤操作记录

    在日常的运维工作中,经常会涉及到在拷贝某个目录时要排查其中的某些文件.废话不多说,下面对这一需求的操作做一记录: linux系统中,假设要想将目录A中的文件复制到目录B中,并且复制时过滤掉源目录A中的 ...

  9. [转]Windows系统中监控文件复制操作的几种方式

    1. ICopyHook 作用: 监视文件夹和打印机移动,删除, 重命名, 复制操作. 可以得到源和目标文件名. 可以控制拒绝操作. 缺点: 不能对文件进行控制. 只对Shell文件操作有效, 对原生 ...

随机推荐

  1. unity导入3dsMax源文件.max

    https://blog.csdn.net/qq_28002559/article/details/53693621 首先unity导入3dsMax文件为什么要使用.max而不用.fbx,因为我不是做 ...

  2. Proteus 仿真运算放大器出现 GMIN 问题

    Proteus 仿真运算放大器出现 GMIN 问题 为了仿真一个反相运算放大器,在仿真时出现 GMIN 问题,将 后面的 4.7UF 去掉就可以正常仿真. 初步检查是因为输入频率太低,输入时我用的是 ...

  3. NoClassDefFoundError: org/apache/juli/logging/LogFactory

    将 apache-tomcat-7.0.8\lib 下的 tomcat-util.jar添加到 注:不是 直接在 configure build path 中添加 jar

  4. HBase之八--(2):HBase二级索引之Phoenix

    1. 介绍 Phoenix 是 Salesforce.com 开源的一个 Java 中间件,可以让开发者在Apache HBase 上执行 SQL 查询.Phoenix完全使用Java编写,代码位于 ...

  5. jredis 客户端 使用

    redis学习及实践3---Jedis.JedisPool.Jedis分布式实例介绍 Java中使用Jedis操作Redis Redis客户端:Jedis

  6. 【游记】noip2017酱油记

    2017-10-14:初赛 12:00:比赛时间14:30-16:30,由于比赛地点在二附中,我12点就坐上了地铁(无力吐槽,周六中午人还那么多,站了一路). 13:45:到了efz,所有人都被拦在了 ...

  7. TIMEQUEST学习之黑金动力(四)

    现在知道时序约束主要是FPGA to ic,或者ic to FPGA. 上图可以表示FPGA to IC, IC to FPGA. fpga2ic:fpga2ext 是 fpga 致 ic 信号的走线 ...

  8. cocos2dx 3.6版本播放动画

    IDE: VS2013 版本:cocos2dx 3.3.6 语言:c++ 11 3.x版本改动与2.x版本相比改动很大,几个比较明显的点就是所有带cc的前缀没有了,然后一些获取类型的函数名称加了get ...

  9. InfluxDB命令使用

    身份验证与授权(权限管理) Authentication and Authorization 注意:身份授权与验证不能用于阻止恶意用户.如果有额外的做合理性和安全性的需求,InfluxDB可以运行在第 ...

  10. 教你用一行Python代码实现并行(转)

    教你用一行Python代码实现并行 本文教你通过一行Python实现并行化. Python在程序并行化方面多少有些声名狼藉.撇开技术上的问题,例如线程的实现和GIL,我觉得错误的教学指导才是主要问题. ...