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. Jenkins修改域认证,非域用户忘记密码处理

    一.认证域地址修改 1. 编辑配置文件 vi $JENKINS_HOME/jenkins/config.xml 2.修改如下内容: <securityRealm class="huds ...

  2. 添加view类图中的二级菜单

    void CFafdsafasdfasfasView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message han ...

  3. 关于asp.net会话阻塞

    现象:在一个网站中,当访问一个处理比较耗时的页面(A页面),页面请求还没有返回时,此时再点击访问该网站的其他页面(B页面)会出现B页面很久都没有响应和返回,直到A页面输出返回数据时才开始处理B页面的请 ...

  4. 在ubuntu14.14 安装php扩展扩展出现的问题

    我是在ubuntu14.14 安装的 lnmp. 部分扩展.均已安装好,但是我用apt-get 方式安装 redis和curl扩展时,我的配置都设置但是从phpinfo里面看没有响应的配置项. 于是我 ...

  5. 一些不太常用的Linux命令

    ACCTCOM 查看所有用户执行过的进程 acctcom | tail - 查看指定用户执行过的进程/命令 acctcom -u <username> | tail - 使用一个正则表达式 ...

  6. 在centos中使用vim编辑器

    下面用编辑crontab举个例子: 在命令行输入 crontab -e 会直接进入vim编辑模式编辑crontab文件. 随后可以输入“i”进入insert模式 然后移动光标在指定的位置添加文字,可以 ...

  7. Swift—Cocoa Touch设计模式-备

    目标(Target)与动作(Action)是iOS和OS X应用开发的中事件处理机制.   问题提出 如图所示是一个ButtonLabelSample案例设计原型图,其中包含一个标签和一个按钮,当点击 ...

  8. 电脑远程控制手机2—webkey

    远程控制神器,这个是真真切切试验成功了.我用的手机是OPPOR817和华为A199. 网上去下载webkey,http://soft.shouji.com.cn/down/23169.html 然后安 ...

  9. uva 10034 Problem A: Freckles

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  10. 【转】Java中 List的遍历

    原文网址:http://blog.csdn.net/player26/article/details/3955906 import java.util.ArrayList; import java.u ...