OC基础  文件管理

1.文件管理类NSFileManager对象的创建:

NSFileManager *fm = [NSFileManager defaultManager];

2.文件操作:

(1)遍历查看目录下的文件:

  a.遍历查看目录下的文件:contentsOfDirectorAtPath:(NSString *)path error:(NSError **)error;

  b.深度遍历,子目录也遍历:subPathsOfDirectorAtPath:(NSString *)path error:(NSError **)error;

(2)创建文件:createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary*)attar;

(3)创建目录:createDirectoryAtPath:(NSString *)path withIntermediateDirectories:

attributes:(NSDictionary *)attributes error:(NSError **)error

(4)拷贝文件/目录:copyItemAtPath:(NSString *)srcPath toPath:(NSString *)dsPath error:  (NSError **)error;

(5)移动文件/目录:moveItemAtPath:(NSString *)srcPath toPath:(NSString *)dsPath error:  (NSError **)error;

(6)删除文件/目录:removeItemAtPath:(NSString *)path error:(NSError **)error;

(7)获取文件属性:attributesOfItemAtPath:(NSString *)path error:(NSError **)error;

(8)判断文件是否存在:fileExistsAtPath:(NSString *)path;

测试:

#import <Foundation/Foundation.h>

#define PATH       @"/Users/hwt/Desktop/test"
#define FILEPATH @"/Users/hwt/Desktop/test/1.txt"
#define NEWPATH @"/Users/hwt/Desktop/test/test1"
#define TOPATH @"/Users/hwt/Desktop/test/test2"
#define MOVEPATH @"/Users/hwt/Desktop/test/test2/2.txt"
#define FILEPATH2 @"/Users/hwt/Desktop/test/2.txt" int main(int argc, const char * argv[])
{ @autoreleasepool { //文件管理类对象的创建
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error;
//文件遍历
//浅度遍历(只遍历当前路径下的文件)
NSArray *array = [fm contentsOfDirectoryAtPath:PATH error:&error];
NSLog(@"array = %@",array); //深度遍历(遍历当前文件夹以及自文件夹里面的所有内容)
NSArray *array1 = [fm subpathsOfDirectoryAtPath:PATH error:&error];
NSLog(@"array1 = %@",array1); NSString *str = @"你好";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
//创建文件,并写入数据
BOOL result = [fm createFileAtPath:FILEPATH contents:data attributes:nil];
if (result) {
NSLog(@"文件创建成功");
}
else {
NSLog(@"文件创建失败");
} //创建目录
result = [fm createDirectoryAtPath:NEWPATH withIntermediateDirectories:YES attributes:nil error:&error];
if (result) {
NSLog(@"文件夹创建成功");
}
else {
NSLog(@"文件夹创建失败");
} //文件/目录拷贝
result = [fm copyItemAtPath:NEWPATH toPath:TOPATH error:&error];
if (result) {
NSLog(@"文件拷贝成功");
}
else {
NSLog(@"文件拷贝失败");
} //文件/目录移动
result = [fm moveItemAtPath:FILEPATH toPath:MOVEPATH error:&error];
if (result) {
NSLog(@"文件移动成功");
}
else {
NSLog(@"文件移动失败");
} //文件或目录删除
result = [fm removeItemAtPath:TOPATH error:&error];
if (result) {
NSLog(@"文件删除成功");
}
else {
NSLog(@"文件删除失败");
} //获取文件属性
NSDictionary *dic = [fm attributesOfItemAtPath:FILEPATH2 error:&error];
NSLog(@"文件属性dic = %@",dic); //获取文件的大小
unsigned long long filesize = [dic fileSize];
NSLog(@"文件的大小filesize = %llu",filesize); //文件是否存在
result = [fm fileExistsAtPath:FILEPATH2];
if (result) {
NSLog(@"文件存在");
}
else {
NSLog(@"文件不存在");
} }
return ;
}

测试结果:

补充:

文件句柄类NSFileHandle

我们知道,对文件的读写都需要用NSFileHandle打开文件,并且读取和写入的数据都是NSData类型的二进制数据,下面让我们来看一下。

1.打开文件的方法有三种:

(1)只读方式打开:fileHandleForReadingAtPath:(NSString *)path;

(2)只写方式打开:fileHandleForWritingAtPath:(NSString *)path;

(3)读写方式打开:fileHandleForUpdatingAtPath:(NSString *)path;

2.读指定长度的数据:readDataOfLength:(NSUinteger)length;

3.从当前偏移量读到文件末尾:readDataToEndOfFile;

4.设置文件的偏移量:seekToFileOffset:(unsigned long long)offset;

5.将文件偏移量定位到文件末尾:seekToEndOfFile;

6.将文件长度设置为offset:truncateFileAtOffset:(unsigned long long)offset;

7.追加写入文件:writeData:(NSData *)data;

8.将缓存区的内容立即同步到磁盘:synchronizeFile;

#import <Foundation/Foundation.h>

#define PATH @"/Users/hwt/Desktop/test/2.txt"

int main(int argc, const char * argv[])
{ @autoreleasepool { //以只读的方式打开文件
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:PATH];
//以只写的方式打开文件
NSFileHandle *fh1 = [NSFileHandle fileHandleForWritingAtPath:PATH];
//以可读写的方式打开文件
NSFileHandle *fh2 = [NSFileHandle fileHandleForUpdatingAtPath:PATH]; //读取数据
//默认情况,光标位置是0
NSData *data =[fh2 readDataToEndOfFile];
//光标位置丁文
[fh2 seekToFileOffset:];
//从光标位置开始,读取指定长度的内容
data = [fh2 readDataOfLength:];
NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"string = %@",string); NSString *str1 = @"你好,hello world";
NSData *data1= [str1 dataUsingEncoding:NSUTF8StringEncoding];
//将数据内容追加到缓存里
[fh2 writeData:data1];
//将缓存区的内容立即同步到文件中
[fh2 synchronizeFile]; }
return ;
}

OC基础 文件管理的更多相关文章

  1. 【OC基础语法考试】

    OC基础语法已经全部学完,但是这些知识只是最基础的,还有很多高级知识,这个可能需要后面慢慢的去学习才能体会到.接下来我会总结前面的OC基础语法,如果大家发现有什么不正确的地方,请指正,小弟是新生,多请 ...

  2. iOS 阶段学习第11天笔记(OC基础知识)

    iOS学习(OC语言)知识点整理 一.OC基础知识 1)#import  用于导入头文件,预处理阶段加载引用,只加载一次. 2)OC 依赖于Foundation框架下的头文件Foundation.h, ...

  3. OC基础笔记目录

    OC基础(1) Objective-C简介 OC和C对比 第一个OC程序 面向对象思想 OC基础(2) 类与对象 类的设计 第一个OC类 对象方法的声明和实现 类方法的声明和实现 OC基础(3) 对象 ...

  4. OC基础 NSDate

    OC基础  NSDate #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @auto ...

  5. OC基础 NSData

    OC基础 NSData 1.NSString转NSData //NSString转NSData NSString *string = @"abcd12345"; NSData *d ...

  6. OC基础 代理和协议

    OC基础 代理和协议 1.协议 (1)oc语言中得协议:一组方法列表,不需要我们自己实现,由遵守协议的类来实现协议所定制的方法. (2)协议的使用步骤:制定协议-->遵守协议-->实现协议 ...

  7. OC基础 内存管理

    OC基础  内存管理 我们所了解的c语言内存管理,如下: (1)c语言的内存分配:char *p = (char*)malloc(100*sizeof(char)); (2)c语言的内存释放:free ...

  8. OC基础 类的三大特性

    OC基础  类的三大特性 OC的类和JAVA一样,都有三大特性:继承,封装,多态,那么我们就来看一下OC中类的三大特性. 1.继承 继承的特点: (1)子类从父类继承了属性和方法. (2)子类独有的属 ...

  9. OC基础 点语法的使用

    OC基础 点语法的使用 1.创建一个Student类继承于NSObject,Student.h文件 #import <Foundation/Foundation.h> @interface ...

随机推荐

  1. VS2013 编译 MySql Connector C 6.1.6

    1.下载cmake http://cmake.org/ 2.下载最新版MySql Connector C http://www.mysql.com 3.命令行下,转到源代码目录下,"cmak ...

  2. 32位和64位adb下载及安装

    一.已安装android sdk时 1.拷贝 从%ANDROID_HOME/platform-tools 下拷贝如下文件到/System32和/SysWOW64下即可. adb.exe AdbWinA ...

  3. eclipse下编译openfire3.9.1源码

    [一].下载源码 打开网址:http://www.igniterealtime.org/downloads/source.jsp 选择目前最新版本 openfire_src_3_9_1.zip 下载. ...

  4. Div在BOdy中居中

    <h1 style="position: absolute; width: 500px; height:200px; left:%; top:%; margin-left:-250px ...

  5. Css溢出隐藏

    display: -webkit-box;-webkit-line-clamp: 2;     /*多少行数之后显示为省略...*/word-wrap: break-word;word-break: ...

  6. 64位系统中开启32位应用,特别是OLEDB

    IIS7 - Running 32-bit and 64-bit ASP.NET versions at the same time on different worker processes IIS ...

  7. 64位linux中使用inet_ntoa报错处理

    最近一直使用linux mint 15,我用的是64位操作系统,在进行网络编程的时候,发现一个问题,请看源码: /*get_ip_by_name.c*/ #include <stdio.h> ...

  8. 8.2.1.10 Nested-Loop Join Algorithms 嵌套循环 关联算法:

    8.2.1.10 Nested-Loop Join Algorithms 嵌套循环 关联算法: MySQL 执行关联在表之间使用一个嵌套循环算法或者变种 Nested-Loop Join Algori ...

  9. cf435C Cardiogram

    C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  10. hadoop2.2.0伪分布模式64位安装

    hadoop2.2.0伪分布模式64位安装用到的软件:jdk-6u45-linux-x64.bin,hadoop-2.2.0.x86_64.tar.gz 修改主机名vim /etc/sysconfig ...