oc中的文件和目录相关操作
来源:互联网 发布:国家进出口数据 编辑:程序博客网 时间:2024/05/30 12:31
1.文件的浅度遍历与深度遍历
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
//NSFileManager * fm = [[NSFileManager alloc]init];//创建文件管理器
//第一步创建一个文件管理器
NSError * error= nil;
//显示路径下的内容,作用类似于ls -a指令
//返回值是把目录下的内容放到NSArray中
//浅度遍历
NSFileManager * fm = [NSFileManager defaultManager];
NSArray * array = [fm contentsOfDirectoryAtPath:@
"/Users/aaaaa/Desktop"
error:&error];
//成功返回nil错误返回失败信息
if
(error)
{
NSLog(@
"Error=%@"
,error);
return
-1;
//代表函数返回(结束函数)
//exit(1);//结束当前进程
}
else
{
NSLog(@
"array=%@"
,array);
}
//深度遍历
//可以显示子目录的内容
NSArray * array1 = [fm subpathsOfDirectoryAtPath:@
"/Users/aaaaa/Desktop"
error:&error];
if
(error)
{
NSLog(@
"Error=%@"
,error);
}
else
{
NSLog(@
"array1=%@"
,array1);
}
2.创建目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
NSFileManager * fm = [NSFileManager defaultManager];
NSError * error = nil;
//当前路径指的是可执行程序的路径而不是代码的路径
//创建目录 返回值:创建成功返回YES 失败返回NO
BOOL
ret = [fm createDirectoryAtPath:@
"./hello2/world"
//创建目录的路径
withIntermediateDirectories:YES
//路径不存在是否创建目录 YES代表创建(会把不存在的目录也创建) NO代表不创建
attributes:nil
//文件属性(权限),通常写nil代表默认权限
error:&error];
if
(ret)
{
NSLog(@
"success!"
);
}
else
{
NSLog(@
"error = %@"
,error);
}
1
2
3
4
5
6
NSFileManager * fm = [NSFileManager defaultManager];
//创建文件
BOOL
ret = [fm createFileAtPath:@
"./1.txt"
//路径以及文件名
contents:nil
//要创建的文件的内容 nil代表空文件 若文件以及存在会覆盖该文件
attributes:nil];
[fm fileExistsAtPath:@
"./1.txt"
]
//判断文件时候存在
1
2
3
4
5
6
7
NSFileManager * fm = [NSFileManager defaultManager];
NSError * error = nil;
//拷贝文件/目录
//第二个参数必须要写全文件或者目录名字
BOOL
ret = [fm copyItemAtPath:@
"/Users/aaaaa/Desktop/string.c"
//要复制的文件的路径
toPath:@
"./1.txt"
//目的文件(文件名不要加空格)若以及存在会返回error
error:&error];
5.移动(重命名)文件/目录
1
2
3
4
5
6
//重命名(移动)文件或目录
//重命名:就是修改文件路径
//如果文件/目录存在 移动失败
ret = [fm moveItemAtPath:@
"/Users/aaaaa/Desktop/"
toPath:@
"./1/1"
//参数要写全称
error:&error];
6.删除文件/目录
1
2
3
4
//删除文件/目录
//谨慎使用(会直接删除 不会丢到废纸篓中)
ret = [fm removeItemAtPath:@
"./1.txt"
error:&error];
7.NSData与字符串
1
2
3
4
5
6
7
NSString * str = @
"hello world"
;
//将OC字符串转换成NSData类型
//NSData 文件缓冲区
NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
//将NSData类型转换成NSString类型
NSString * str2 = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
8.文件读写操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//文件读写操作
//第一步打开文件
//FILE * fp = fopen("./1.txt","w");文件指针
//文件句柄
//以只读方式打开
NSFileHandle * fh = [NSFileHandle fileHandleForReadingAtPath:@
"/Users/aaaaa/Desktop/string.c"
];
//以只写方式打开
fh = [NSFileHandle fileHandleForWritingAtPath:@
"/Users/aaaaa/Desktop/string.c"
];
//以读写方式打开
fh = [NSFileHandle fileHandleForUpdatingAtPath:@
"/Users/aaaaa/Desktop/string.c"
];
//OC的打开函数都会把偏移量定位在未见首
//若想在文件尾进行操作,可将文件偏移量定位到文件尾
[fh seekToEndOfFile];
//定位到文件尾
NSData * data1 = [fh readDataOfLength:10];
//读取指定个数字节
NSData * data2 = [fh readDataToEndOfFile];
//从当前偏移量读到文件尾
[fh seekToFileOffset:10];
//把文件偏移量定位到10字节
9.向文件中写数据
1
2
3
4
5
NSFileHandle * fh2 = [NSFileHandle fileHandleForUpdatingAtPath:@
"./1.txt"
];
NSString * str = @
"hello world"
;
NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];
//写数据必须要转化为NSData
[fh2 writeData:data];
10.获取文件长度
1
2
3
4
5
6
7
//通过文件管理器获取文件属性然后通过字典获取文件长度
NSFileManager * fm = [NSFileManager defaultManager];
NSDictionary * dict = [fm attributesOfItemAtPath:@
"/Users/aaaaa/Desktop/string.c"
error:&error];
//方法一:
NSLog(@
"size = %lld"
,[dict fileSize]);
//方法二:
NSLog(@
"%@"
,[dict objectForKey:NSFileSize]);
0 0
- oc中的文件和目录相关操作
- 【OC】文件操作相关
- OC中的文件操作
- 文件,目录相关操作
- Python中的文件和目录操作
- Python中的文件和目录操作
- Python中的文件和目录操作
- Python中的文件和目录操作
- Python中的文件和目录操作
- Python中的文件和目录操作实现
- OC学习之文件目录操作
- OC学习之文件目录操作
- linux 文件和目录操作的相关函数
- Linux教程 之----文件和目录操作相关命令
- Linux 文件和目录的操作,权限相关命令
- Linux 文件和目录操作的相关函数
- Linux 文件和目录操作的相关函数
- 解析linux 文件和目录操作的相关函数
- struts2 基于xml 配置文件的输入验证
- springMVC --配置详细与注解说明
- mimeType形式大全
- Node.js 使用 child_process 实现多进程
- mysql 递归查询的方法
- oc中的文件和目录相关操作
- dex文件结构分析-3
- linux查看用户命令
- 安装xmind出错:没有JRE
- LeetCode223——Rectangle Area
- 传入时间与现在时间的差距及其表示
- android事件传递机制的浅谈
- 高效学习有方法--自己的感悟
- Snail—UI学习之表视图TableView多行删除