OBjective-C:文件管理类NSFileManager
文件管理类NSFileManager类:对文件进行创建、复制、重命名、删除等,一般不对文件内容进行操作。
NSData类和NSMutableData类:相当于数据缓冲区
| 方法 | 描述 |
|---|---|
| -(NSData ) contentsAtPath: *path | 从一个文件读取数据 |
| -(BOOL) createFileAtPath: path contents: (NSData ) *data attributes: attr | 向一个文件写入数据 |
| -(`BOOL) removeItemAtPath: path error: err | 删除一个文件 |
| -(BOOL) moveItemAtPath: from toPath: to error: err | 重命名或移动一个文件(to不能是已存在的) |
| -(BOOL) copyItemAtPath: from toPath: to error: err | 复制一个文件(to不能是已存在的) |
| -(BOOL) contentsEqualAtPath: path1 andPath: path2 | 比较这两个文件的内容 |
| -(BOOL) fileExistsAtPath: path | 测试文件是否存在 |
| -(BOOL) isReadableFileAtPath: path | 测试文件是否存在且是可读文件 |
| -(BOOL) isWritableFileAtPath: path | 测试文件是否存在且是可写文件 |
| -(NSDictionary ) attributesOfItemAtPath: *path error: err | 获取文件的属性 |
| -(BOOL) setAttributesOfItemAtPath: attr error: err | 更改文件的属性 |
| —————-目录—————- | —————目录—————- |
| -(NSString *) currentDirectoryPath | 获取当前目录 |
| -(BOOL) changeCurrentDirectoryPath: path | 更改当前目录 |
| -(BOOL) copyItemAtPath: from toPath: to error: err | 复制目录结构(to不能是已存在的) |
| -(BOOL) createDirectoryAtPath: path withIntermediateDirectories: (BOOL) flag attributes: attr | 创建一个新目录 |
| -(NSArray ) contentsOfDirectoryAtPath: *path error: err | 列出目录内容 |
| -(NSDirectoryEnumerator ) enumeratorAtPath: *path | 枚举目录的内容 |
| -(BOOL) removeItemAtPath: path error: err | 删除空目录 |
| -(BOOL) moveItemAtPath: from toPath: to error: err | 重命名或移动一个目录(to不能是已存在的) |
NSFileManager *fm = [NSFileManager defaultManager];
NSFileManager *fm2 = [NSFileManager defaultManager]; NSLog(@"%@,%@",fm,fm2);
-- ::23.744 -NSFlleManager[:] <NSFileManager: 0x100306940>,<NSFileManager: 0x100306940>
2、获取当前路径
NSString *currentPath = [fm currentDirectoryPath];
NSLog(@"当前目录:%@0",currentPath);
-- ::15.624 -NSFlleManager[:] 当前目录:/Users/mac/Library/Developer/Xcode/DerivedData/-NSFlleManager-bjtgjiggpfdimgemqrewbfyjlwwu/Build/Products/Debug0
Program ended with exit code:
3、改变当前路径
[fm changeCurrentDirectoryPath:@".."];
currentPath = [fm currentDirectoryPath];
NSLog(@"当前目录:%@0",currentPath);
-- ::54.837 -NSFlleManager[:] 当前目录:/Users/mac/Library/Developer/Xcode/DerivedData/-NSFlleManager-bjtgjiggpfdimgemqrewbfyjlwwu/Build/Products0
Program ended with exit code:
4.创建目录
NSError *error;
[fm createDirectoryAtPath:@"temp" withIntermediateDirectories:YES attributes:nil error:&error];
if(error)
{
NSLog(@"创建目录错误:%@",error);
}
else
{
NSLog(@"创建成功");
} [fm changeCurrentDirectoryPath:@"temp"];
currentPath = [fm currentDirectoryPath];
NSLog(@"当前目录:%@0",currentPath);
-- ::48.230 -NSFlleManager[:] 创建成功
-- ::48.230 -NSFlleManager[:] 当前目录:/Users/mac/Library/Developer/Xcode/DerivedData/-NSFlleManager-bjtgjiggpfdimgemqrewbfyjlwwu/Build/Products/temp0
Program ended with exit code:
5.复制文件(目录)
[fm copyItemAtPath:@"../Debug/01-NSFlleManager" toPath:@"./02-NSFlleManager" error:&error];
if(error)
{
NSLog(@"复制文件失败:%@",error);
}
else
{
NSLog(@"复制文件成功");
}
6.移动文件(目录),在同一个目录下改名操作
[fm moveItemAtPath:@"02-NSFlleManager" toPath:@"03-NSFlleManager" error:&error];
if(error)
{
NSLog(@"文件移动失败:%@",error);
}
else
{
NSLog(@"文件移动成功");
}
7.删除文件(目录)
error = nil;
[fm removeItemAtPath:@"../01-NSFlleManager" error:&error];
if(error)
{
NSLog(@"文件删除失败:%@",error);
}
else
{
NSLog(@"文件删除成功");
}
8.枚举目录内容
8.1 递归输出所有目录的内容
NSDirectoryEnumerator *dirEnumerator = [fm enumeratorAtPath:@"/Users/mac/IOS/OC"];
NSString *path;
while(path = [dirEnumerator nextObject])
{
NSLog(@"%@",path);
}
8.2 .只输出当前目录的内容
NSArray *files = [fm contentsOfDirectoryAtPath:@"/Users/mac/IOS/OC" error:&error];
NSLog(@"%@",files);
9.列出目录(文件)的属性
NSDictionary *attr = [fm attributesOfItemAtPath:@"/Users/mac/IOS/OC" error:&error];
NSLog(@"%@",attr);
-- ::56.755 -NSFlleManager[:] {
NSFileCreationDate = "2015-08-10 01:39:40 +0000";
NSFileExtensionHidden = ;
NSFileGroupOwnerAccountID = ;
NSFileGroupOwnerAccountName = staff;
NSFileModificationDate = "2015-08-24 01:22:42 +0000";
NSFileOwnerAccountID = ;
NSFileOwnerAccountName = mac;
NSFilePosixPermissions = ;
NSFileReferenceCount = ;
NSFileSize = ;
NSFileSystemFileNumber = ;
NSFileSystemNumber = ;
NSFileType = NSFileTypeDirectory;
}
Program ended with exit code:
10.创建文件
NSString *str = @"this is a test";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
if([fm createFileAtPath:@"1.txt" contents:data attributes:nil])
{
NSLog(@"创建文件成功");
}
else
{
NSLog(@"创建文件失败");
}
11.判断文件是否存在
if([fm fileExistsAtPath:@"1.txt" isDirectory:NULL])
{
NSLog(@"文件存在");
}
else
{
NSLog(@"文件不存在");
}
12.读取文件内容
NSData *data2 = [fm contentsAtPath:@"1.txt"];
NSLog(@"%ld",[data2 length]);
NSString *str2 = [[NSString alloc]initWithData:data2 encoding:NSUTF8StringEncoding];
NSLog(@"%@",str2);
-- ::33.660 -NSFlleManager[:] this is a test
Program ended with exit code:
OBjective-C:文件管理类NSFileManager的更多相关文章
- 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)
========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...
- UI进阶 文件管理器(NSFileManager)文件对接器(NSFileHandle)
一.文件管理器与文件连接器之间的区别 文件管理器(NSFileManager) 此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取. 文件连接器(NSFileHandle) 此类主要是 ...
- C# 文件管理类 Directory
今天简单接触了一下C#的文件管理类,对类的大体功能做了简单的了解; 做项目用于判断文件是否存.在创建文件.删除文件较为常用:今天大体总结文件调取功能: public string GetFile() ...
- Linux上的文件管理类命令(2)
Linux上的文件管理类命令都有哪些,其常用的使用方法及其相关示例演示: wc 统计文件中的字节数.单词数.行数.并将统计结果显示输出 -c, --bytes 打印字节数 [root@qingchen ...
- 【原】iOS学习之文件管理器(NSFileManager)和文件对接器(NSFileHandle)
1.文件管理器(NSFileManager) 1> 主要作用及功能方法 主要作用:此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取. 功能方法: 2> 创建文件夹 创建所 ...
- Linux文件管理类命令及命令别名
文件查看类命令: cat: tac: 从文件尾部开始显示 分屏显示: more [option] 文件名: 查看至文件尾部会退出 空格为翻页 less [option] 文件名: 查看至文件尾部不退出 ...
- 3-1 Linux文件管理类命令详解
根据马哥Linux初级 03-01整理 1. 目录管理 ls cd pwd mkdir rmdir tree 2. 文件管理 touch stat file rm cp mv nano 3. 日期时间 ...
- Objective - C - 添加类目 - NSDate
1.类目为系统内部的类或者是没有源代码的类添加方法,不有添加实例变量 2.添加的方法会成为原类的一部分,子类照样可以使用 3.类目的文件名为原类名+文件名 4.既可以添加实例方法,也可以添加类方法 X ...
- 一道题看懂OC的文件管理:NSFileManager,计算文件包含内存大小
计算文件夹下所有文件的大小 // 查看错误信息 __autoreleasing NSError *error; // 文件管理对象 NSFileManager *manager = [NSFileMa ...
随机推荐
- Centos 7.2 安装 Python 3.5(适用于Python 3所有版本安装)
提示:我们必须不能破坏系统的环境.因为几个关键的应用程序依赖于Python 2.7.5(centos 7默认版本).如果替换了系统的Python环境就会发生很多难以预见的错误,甚至要重装系统. 安装前 ...
- Python之路【第十一篇】: 进程与线程
阅读目录 一. cpython并发编程之多进程1.1 multiprocessing模块介绍1.2 Process类的介绍1.3 Process类的使用1.4 进程间通信(IPC)方式一:队列1.5 ...
- ES6 一些笔记
一. let/const: 1. “暂时性死区”概念:在代码块内,使用let/const命令声明变量之前,该变量都是不可用的.这在语法上,称为“暂时性死区”(temporal dead zone,简称 ...
- java中int和Integer比较
java中int和Integer比较 一,类型区别 我们知道java中由两种数据类型,即基本类型和对象类型,int就是基本数据类型,而Integer是一个class,也习惯把Integer叫做int的 ...
- mongdb 拓展的下载地址和编译安装(php)
下载地址:https://pecl.php.net/package/mongodb 编译安装: $ tar zxvf mongodb-mongodb-php-driver-<commit_id& ...
- mongodb中获取图片文件<标记>
获取图片文件 @RequestMapping(value="/downLoadFileFormMongo.do",method=RequestMethod.GET) @Respon ...
- 【SQL】180. Consecutive Numbers
Write a SQL query to find all numbers that appear at least three times consecutively. +----+-----+ | ...
- iOS 11开发教程(十二)iOS11应用视图始祖——UIView
iOS 11开发教程(十二)iOS11应用视图始祖——UIView 在Swift中,NSObject是所有类的根类.同样在UIKit框架(UIKit框架为iOS应用程序提供界面对象和控制器)中,也存在 ...
- dSploitzANTI渗透教程之HTTP服务重定向地址
dSploitzANTI渗透教程之HTTP服务重定向地址 HTTP服务 HTTP服务主要用于重定向地址的.当用户创建一个钓鱼网站时,可以通过使用HTTP服务指定,并通过实施中间人攻击,使客户端访问该钓 ...
- JQuery的源码阅读
探索原理,animation实现,一个对象可以同时绑定多个事件,这是如何实现的? (function(window, undefined) { function jQuery(selector){ r ...