1.关于文件的介绍

-》什么是文件:

文件概念, 广义文件。 狭义文件(磁盘文件)。

文件常见的使用操作(可用命令行演示文件操作的使用场景)。

-》什么是路径:

简单来说就是,在系统中,要找某个文件,所要经历的文件夹阶梯。

2. 文件管理类 NSFileManager(系统类库

-》NSFileManager是一个单例类 (对于一个单例类。不论实例化对象多少次,都仅仅有一个对象实例。并且这个实例是全局的,能被整个系统訪问到, 就像全局变量一样能够被整个project所共享它的数据。)

要对文件进行管理,必需要先获取文件管理器

(NSFileManager类)的对象

NSFileManager* fm = [NSFileManager defaultManager];

-》对文件进行管理的各种操作:

a. 遍历文件夹下的文件(文件夹下的文件名称列表)(查)

b. 创建文件或文件夹(增)

c. 复制文件或文件夹(增)

d. 移动文件或文件夹(改)

e. 删除文件或文件夹(删)

-》创建文件管理器单例对象

NSFileManager * fm = [NSFileManager defaultManager];

-》遍历文件夹下的内容

//浅度遍历

- (NSArray *)contentsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;

//深度遍历

- (NSArray *)subpathsOfDirectoryAtPath:(NSString *)path error:(NSError **)error;

-》创建文件

//创建普通文件

- (BOOL)createFileAtPath:(NSString *)path contents:(NSData *)data attributes:(NSDictionary *)attr;

//创建文件夹

- (BOOL)createDirectoryAtPath:(NSString *)path

withIntermediateDirectories:(BOOL)createIntermediates

attributes:(NSDictionary *)attributes

error:(NSError **)error;

-》复制文件/文件夹

- (BOOL)copyItemAtPath:(NSString *)srcPath

toPath:(NSString *)dstPath

error:(NSError **)error;

-》移动文件/文件夹

- (BOOL)moveItemAtPath:(NSString *)srcPath

toPath:(NSString *)dstPath

error:(NSError **)error;

-》删除文件/文件夹

- (BOOL)removeItemAtPath:(NSString *)path

error:(NSError **)error;

-》获取文件属性//文件的元信息

- (NSDictionary *)attributesOfItemAtPath:(NSString*)path

error:(NSError **)error;

-》推断文件是否存在

- (BOOL)fileExistsAtPath:(NSString *)path;

拓展: 单例模式

(1)单例模式是一种经常使用的设计模式。

在应用这个模式时,单例对象的类必须保证仅仅有一个实例存在。并且自行实例化并向整个系统提供这个实例。

也就是说,对于一个单例类。不论实例化对象多少次。都仅仅有一个对象实例。并且这个实例是全局的。能被整个系统訪问到, 就像全局变量一样能够被整个project所共享它的数据。

(2)非标准的单例设计(单例模式的一种可选实现)

单例的创建方法通常以default或shared前缀开头

+ (MyPlane *)defaultPlane{

//或者 + (MyPlane *)sharedPlane{

    static MyPlane * plane = nil;

    @synchronized(self){

           if (!plane) {

            plane = [[self alloc] init];

           }

    }

    return plane;

}

eg.演示样例代码

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{ @autoreleasepool {
NSArray* myDirAarry = nil; //创建文件管理器的单例对象
NSFileManager* fm = [NSFileManager defaultManager];
//浅度遍历
myDirAarry = [fm contentsOfDirectoryAtPath:@"/Users/sub/Desktop/" error:nil];
NSLog(@"%@",myDirAarry);
//深度遍历
myDirAarry=[fm subpathsOfDirectoryAtPath:@"/Users/sub/Desktop" error:nil];
NSLog(@"%@",myDirAarry); [fm createFileAtPath:@"/Users/sub/Desktop/haha.txt" contents:nil attributes:nil]; [fm createDirectoryAtPath:@"/Users/sub/Desktop/testdir" withIntermediateDirectories:NO attributes:nil error:nil];
//再次进行浅度遍历
myDirAarry = [fm contentsOfDirectoryAtPath:@"/Users/sub/Desktop/" error:nil];
NSLog(@"%@",myDirAarry);
//复制文件
[fm copyItemAtPath:@"/Users/sub/Desktop/test.txt" toPath:@"/Users/sub/Desktop/test.rtf" error:nil];
//再次进行浅度遍历
myDirAarry = [fm contentsOfDirectoryAtPath:@"/Users/sub/Desktop/" error:nil];
NSLog(@"%@",myDirAarry);
//移动文件
[fm moveItemAtPath:@"/Users/sub/Desktop/test.txt" toPath:@"/Users/sub/Desktop/testdir/test.txt" error:nil];
//再次进行深度遍历
myDirAarry = [fm subpathsOfDirectoryAtPath:@"/Users/sub/Desktop/" error:nil];
NSLog(@"%@",myDirAarry);
//获取指定文件的属性信息
NSDictionary* fileAttributesInfo = [fm attributesOfItemAtPath:@"/Users/sub/Desktop/test.rtf" error:nil];
NSLog(@"%@",fileAttributesInfo);
if ([fm fileExistsAtPath:@"/Users/sub/Desktop/testdir/test.rtf"]) {
[fm removeItemAtPath:@"/Users/sub/Desktop/testdir/test.rtf" error:nil];
}else{
NSLog(@"No such file");
}
//再次进行深度遍历
myDirAarry = [fm subpathsOfDirectoryAtPath:@"/Users/sub/Desktop/" error:nil];
NSLog(@"%@",myDirAarry); NSDictionary* dic = [fm attributesOfItemAtPath:@"/Users/sub/Desktop/test.rtf" error:nil]; NSLog(@"%@",[dic objectForKey:@"NSFileType"]); }
return 0;
}

3. 文件句柄类 NSFileHandle(系统类库)

操作系统返回给我们程序的文件指针(文件手柄)

  • 对文件进行读写首先须要NSFileHandle打开文件
  • NSFileHandle都是用NSData类型的二进制数据。对文件进行读写的
  • NSData与NSString之间的相互转换

操作方法:

-》打开文件方法

//以仅仅读方式打开

+ (id)fileHandleForReadingAtPath:(NSString *)path;

//以仅仅写方式打开

+ (id)fileHandleForWritingAtPath:(NSString *)path;

//以读写方式打开

+ (id)fileHandleForUpdatingAtPath:(NSString *)path;

-》读指定长度的数据

- (NSData *)readDataOfLength:(NSUInteger)length;

文件的偏移量:又称为文件的读写指针。或称为位置指针。事实上就是,该位置偏移文件開始处的偏移字符数量,是个无符号长整型的数据

-》从当前偏移量读到文件尾

- (NSData *)readDataToEndOfFile;

-》设置文件偏移量

- (void)seekToFileOffset:(unsigned long long)offset;

-》将文件偏移量定位到文件尾

- (unsigned long long)seekToEndOfFile;

-》将文件的长度设置为offset大小(单位为字节)

- (void)truncateFileAtOffset:

(unsigned long long)offset;

-》写文件

- (void)writeData:(NSData *)data;

-》将缓冲区内容马上同步写入磁盘

- (void)synchronizeFile;

eg.演示样例代码

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{ @autoreleasepool {
NSString* myString = @"happy today";
//对于信息数据进行按指定格式的编码,以便写入文件
NSData* aData = [myString dataUsingEncoding:NSUTF8StringEncoding]; NSMutableString* bString = [[NSMutableString alloc] initWithData:aData encoding:NSUTF8StringEncoding]; [bString appendString:@",HELLO WORLD"];
NSLog(@"%@",bString); aData = [bString dataUsingEncoding:NSUTF8StringEncoding]; NSFileManager* fm = [NSFileManager defaultManager]; [fm createFileAtPath:@"/Users/sub/Desktop/new.txt" contents:aData attributes:nil];
//将文件以读写方式打开(以可更新方式打开)
NSFileHandle* fh = [NSFileHandle fileHandleForUpdatingAtPath:@"/Users/sub/Desktop/new.txt"];
//对文件进行读操作
aData = [fh readDataOfLength:3];
NSString* cString = [[NSString alloc] initWithData:aData encoding:NSUTF8StringEncoding]; NSLog(@"%@",cString);
[fh seekToFileOffset:10];
aData = [fh readDataToEndOfFile];
NSString* dString = [[NSString alloc] initWithData:aData encoding:NSUTF8StringEncoding];
NSLog(@"%@",dString); //将文件清空
[fh truncateFileAtOffset:0];
//将编码后的数据写入文件
[fh writeData:aData];
[fh synchronizeFile];
}
return 0;
}

拓展: NSData(数据类)

-》把字符串转化为NSData

NSString *str = @"welcome to myhome";

NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];

-》将NSData转化为字符串

NSString *newStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

OC语言--NSFileManager&amp; NSFileHandle的更多相关文章

  1. swift调用oc语言文件,第三方库文件或者自己创建的oc文件——简书作者

    Swift是怎样调用OC的第三方库的呢?请看下面详情: 情况一: 1.首先打开Xcode,iOS->Application->Single View Application, 选Next. ...

  2. OC语言前期准备

    OC语言前期准备 一.OC简介 Oc语言在c语言的基础上,增加了一层最小的面向对象语法,完全兼容C语言,在OC代码中,可以混用c,甚至是c++代码. 可以使用OC开发mac osx平台和ios平台的应 ...

  3. OC语言基础知识

    OC语言基础知识 一.面向对象 OC语言是面向对象的,c语言是面向过程的,面向对象和面向过程只是解决问题的两种思考方式,面向过程关注的是解决问题涉及的步骤,面向对象关注的是设计能够实现解决问题所需功能 ...

  4. OC语言@property @synthesize和id

    OC语言@property @synthesize和id 一.@property @synthesize关键字 注意:这两个关键字是编译器特性,让xcode可以自动生成getter和setter的声明 ...

  5. OC语言构造方法

    OC语言构造方法 一.构造方法 (一)构造方法的调用 完整的创建一个可用的对象:Person *p=[Person new]; New方法的内部会分别调用两个方法来完成2件事情,1)使用alloc方法 ...

  6. OC语言类的本质和分类

    OC语言类的深入和分类 一.分类 (一)分类的基本知识  概念:Category  分类是OC特有的语言,依赖于类. 分类的作用:在不改变原来的类内容的基础上,为类增加一些方法. 添加一个分类: 文件 ...

  7. OC语言description方法和sel

    OC语言description方法和sel 一.description方法 Description方法包括类方法和对象方法.(NSObject类所包含) (一)基本知识 -description(对象 ...

  8. OC语言BLOCK和协议

    OC语言BLOCK和协议 一.BOLCK (一)简介 BLOCK是什么?苹果推荐的类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. BOLCK和函数的相似 ...

  9. OC语言-03-OC语言-三大特性

    一.封装 1> 封装的定义 隐藏对象的属性和实现细节,仅对外公开接口,控制在程序中属性的读和修改的访问级别 2> 封装的好处 可以通过set方法防止为成员变量设置不合理的值 仅向外部提供公 ...

随机推荐

  1. 转: 解决MSYS2下的中文乱码问题

    解决方案 新建/usr/bin/win: 12 #!/bin/bash$@ |iconv -f gbk -t utf-8 新建/etc/profile.d/alias.sh: 12345678 ali ...

  2. freemarker 如何获得list的索引值

    <#list toplist as toplists> ${toplists_index} </#list> 相当方便

  3. [原]Unity3D深入浅出 - 常用类的成员变量和成员函数(Tranform、Time、Random、Mathf、Input)

    Transform的成员变量 Transform的成员函数 Time类,获取和时间相关的信息,可用来计算帧速率,调整时间流逝的速度等. Random类,可用来生成随机数,随机点和旋转. Mathf类提 ...

  4. jquery提示气泡

    <link href="css/manhua_hoverTips.css" type="text/css" rel="stylesheet&qu ...

  5. BZOJ_1021_[SHOI2008]_Debt循环的债务_(DP)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1021 三个人相互欠钱,给出他们每个人各种面额的钞票各有多少张,求最少需要传递多少张钞票才能把账 ...

  6. Java [Leetcode 43]Multiply Strings

    题目描述: Given two numbers represented as strings, return multiplication of the numbers as a string. No ...

  7. 【Struts】strust.xml中<result type="">所有类型详解

    在默认时,<result>标签的type属性值是“dispatcher”(实际上就是转发,forward).开发人员可以根据自己的需要指定不同的类型,如redirect.stream等.如 ...

  8. Jquery拖拽原理

    /* onmousedown : 选择元素 onmousemove : 移动元素 onmouseup : 释放元素 */ 查看Demo:拖拽图片 function drag(obj) { obj.on ...

  9. LA 6540 Fibonacci Tree

    以前做过的题···重新做一遍之后怎么做怎么wa···后来GG了···第二天看不知道为啥A了···难道我失忆了? 题意:无向图,边有黑色和白色两种颜色,求是否存在一个生成树中白边的个数是斐波那契数. 解 ...

  10. HTML DOM 教程Part1

    2015-05-08 摘自W3C School HTML DOM HTML DOM 定义了访问和操作HTML文档的标准方法.HTML DOM 把 HTML 文档呈现为带有元素.属性和文本的树结构(节点 ...