您的位置:首页 > 其它

Foundation文件操作

2015-06-08 11:53 204 查看

1 NSFileManager

NSFileManger使用NSString作为路径名,可以是绝对路径或相对路径。相对路径就是当前目录。

绝对路径以“/”开头,“/”,表示根目录。

常用方法:



NSFileManager *fm = [NSFileManager defaultManager];
//删除文件
if([fm removeItemAtPath:@"abc" error:NULL]==NO){
NSLog("%@","删除失败");
}
//判断是否存在
if( [fm fileExistsAtPath:@"test"]==NO )
//创建副本
if( [fm copyItemAtPath:@"test" toPath:@"newfile" error:NULL]==YES )
//判断是否相等
if( [frm contentsEqualAtPath:@"test" toPath:@"newFile"]==YES )
//重命名
if( [frm moveItemAtPath:@"oldfile" toPath:@"newFile" error:NULL]==YES )
//获取属性
NSDictionary *attr = [frm attributesOfItemPath:@"newFile" error:NULL];
NSLog("%li",[attr objectForKey:NSFileSize]);
//删除
if([ frm removeItemAtPath:@"oldFile" error:NULL]==YES)
//文件内容
NSLog( "%@",[frm stringWithContentsOfFile:@"file" encoding:NSUTF8StringEncoding error:NULL] );


2 缓冲区NSData

NSData可以设置缓冲区,将文件内容读入缓冲区中,或将文件内容写到文件中。32位环境下最多缓冲2G,64位缓冲8EB(8亿G)。

不可变缓冲区NSData。

NSFileManage *fm = [NSFileManage defaultManager];
NSData *data;
data = [fm contentsAtPath:@"file"];
if( [fm createFileAtPath:@"file" contents:data attributes:nil]==NO ){
NSLog("%@","失败");
}


可变缓冲区NSMutableData。

 

3 目录

NSFileManager可以操作目录。



枚举目录内容:

NSFileManage *fm = [NSFileManage defaultManager];
NSString *str = [fm currentDirectoryPath];
NSDirectoryEnumerator *dirEnum;
dirEnum = [fm enumeratorAtPath:str];

while( (path = [dirEnum nextObject]) != nil ){
NSLog(@"%@",path);
}

NSArray *arr = [fm contentsOfDirectoryAtPath:[fm currentPath] error:NULL];
for(path in arr){
NSLog(@"%@",path);
}


4 常用路径方法





5 NSProcessInfo

ProcessInfo可以获取到系统进程的内容,常用方法:


 

6 NSFileHandle

NSFileHandle的常用方法如下:





NSFileHandle可用于标准输入输出:fileHandleWithDevice,device可以是standardInput,standardOutput,standardError,NULLDevice。

注意:NSFileHandle并没有提供创建文件的方法,所以它操作的是已经存在的文件,所以fileHandleForWritingAtPath 和 fileHandleForUpdatingAtPath操作的都是存在的文件,如果不存在就会返回nil。

NSFileHandle *inFile = [NSFileHandle fileHandleWithReadingAtPath:@"testFile"];
if(handle==nil){
return 1;
}
[[NSFileManager defaultManager] createFileAtPath:@"testout" contents:nil attributes:nil];
NSFileHandle *outFile = [FileHandle fileHandleForWritingAtPath:@"testout"];
//长度截断为0
[outFile truncateFileAtOffset:0];

NSData *data = [inFile readDataToEndOfFile];
[outFile writeData:data];

//关闭文件流
[inFile closeFile];
[outFile closeFile];

readDataToEndOfFile方法一次从文件中读取UNIT_MAX字节数据,定义在limits.h,在大多数系统中等于FFFFFFFF。

readDataOfLength可以设置一次读取字节数。

如果读取到文件结尾,方法将返回一个空的NSData,可以使用length方法判断是否读取到数据。

如果要更新一个文件,偏移量被设置为文件的开始,可以通过seekToFileOffSet

NSFileHandle *inFile = [NSFileHandle fileHandleWithReadingAtPath:@"testFile"];
NSFileHandle *outFile = [NSFileHandle fileHandleWithWritingAtPath:@"fileB"];

[outFile seekToEndOfFile];
NSData *data = [inFile readDataToEndOfFile];
[outFile writeData:data];

[inFile closeFile];
[outFile closeFile];


7 NSURL

NSURL *url = [NSURL URLWithString:@"http://google.com"];
NSString *content = [NSString stringWithContentsOfURL:url encoding:NSASCIIStringEncoding error:NULL];

NSDictionary *dict = [NSDictionary dictionayWithContentsOfURL];
NSArray *arr = [NSArray arrayWithContentsOfURL];
NSData *data = [NSData dataWithContentsOfURL];


8 NSBundle简介

bundle是一个目录,其中包含了程序会使用到的资源。我们的程序是一个bundle. 在Finder中,一个应用程序看上去和其他文件没有什么区别. 但是实际上它是一个包含了nib文件,编译代码,以及其他资源的目录. 我们把这个目录叫做程序的main bundle。

NSBundle *myBundle = [NSBundle mainBundle];  //获取main bundle。

NSBundle *bundle = [NSBundlebundleWithPath:@"~/.myApp/Good.bundle"];

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Foundation IO