//获取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. 修改 CentOS 中的 repo

    修改 CentOS 中的 repo 原因 由于安装 odoo 10 太慢,因为服务器是国外的. 修改 进入 repo 文件夹 cd /etc/yum.repos.d/ ls 可以看到所有的源.

  3. win7 + python2.7 安装scipy

    问题: 直接pip install scipy将不能正确安装,缺少文件 方法: 下载  "scipy‑0.19.0‑cp27‑cp27m‑win_amd64.whl"[90多M] ...

  4. bc显示小数点前的0

    bc是强大而常用的计算工具.不过在除法运算时,如果得到的结果值小于1,得到的小数前面的0不存.本篇提供几个常用小数点前缺0的解决方法. [root@maqing ~]# bc bc Copyright ...

  5. 一张图看懂高通QC1.0-QC4.0快充进化之路!QC2.0跟QC3.0充电区别

    快充技术日新月异,快充市场百家争鸣的今天,高通QC快充依然主导着市场.如今QC快充已发展到第四代,每一代都有着革命性的进步.从QC1.0到QC4.0更新换代时间之短,不免让广大人民群众抱怨. “啥?老 ...

  6. Mac brew安装MongoDB

    brew简介安装 brew 又叫Homebrew,是Mac OSX上的软件包管理工具,能在Mac中方便的安装软件或者卸载软件, 只需要一个命令, 非常方便 brew类似ubuntu系统下的apt-ge ...

  7. Oracle ASM操作管理

    查看ASM磁盘情况 SQL> select group_number,disk_number,mount_status,header_status,mode_status,state,failg ...

  8. 14_java之变量|参数|返回值|修饰符

    01java中的文档注释和制作 * A: 在eclipse使用时,可以配合文档注释,导出对类的说明文档,从而供其 他人阅读学习与使用. 通过使用文档注释,将类或者方法进行注释用@简单标注基本信息.如@ ...

  9. [Z] 用GDB调试程序

    原文:http://blog.csdn.net/haoel/article/details/2879 用GDB调试程序 GDB概述———— GDB是GNU开源组织发布的一个强大的UNIX下的程序调试工 ...

  10. Matplotlib 库 : 绘图和可视化

    一.Matplotlib基础知识 1.1Matplotlib中的基本图表包括的元素 x轴和y轴 axis水平和垂直的轴线 x轴和y轴刻度 tick刻度标示坐标轴的分隔,包括最小刻度和最大刻度 x轴和y ...