一、NSFileManager
这个类的主要功能是对文件进行操作:创建,复制,剪切,删除等
- #import <UIKit/UIKit.h>
- #import "AppDelegate.h"
-
-
- int main(int argc, charchar * argv[]) {
- @autoreleasepool {
-
-
-
- NSString *homePath = NSHomeDirectory();
-
- NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
-
- NSFileManager *manager = [NSFileManager defaultManager];
- NSString *str = @"无线互联";
- NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
-
- BOOL sucess = [manager createFileAtPath:filePath contents:data attributes:nil];
- if(sucess){
- NSLog(@"文件创建成功");
- }else{
- NSLog(@"文件创建失败");
- }
-
-
- NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/file"];
- NSError *error;
-
- BOOL success1 = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:YES attributes:nil error:&error];
- if(!success1){
- NSLog(@"创建成功");
- }else{
- NSLog(@"创建失败");
- }
-
-
-
-
- NSData *datas = [manager contentsAtPath:filePath];
- NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"%@",s);
-
-
-
-
-
- NSString *targetPath = [homePath stringByAppendingPathComponent:@"Documents/file/file2.text"];
- BOOL sucess2 = [manager moveItemAtPath:filePath toPath:targetPath error:nil];
- if(sucess2) {
- NSLog(@"移动成功");
- }else{
- NSLog(@"移动失败");
- }
-
-
-
- BOOL sucess3 = [manager copyItemAtPath:filePath toPath:targetPath error:nil];
- if(sucess3){
-
- }else{
-
- }
-
-
-
-
- BOOL isExist = [manager fileExistsAtPath:filePath];
- if(isExist){
- BOOL sucess4 = [manager removeItemAtPath:filePath error:nil];
- if(sucess4){
-
- }else{
-
- }
- }
-
-
-
- NSDictionary *dic = [manager attributesOfItemAtPath:filePath error:nil];
- NSLog(@"%@",dic);
-
- return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
- }
- }
1、创建文件
-
-
- NSString *homePath = NSHomeDirectory();
-
- NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
-
- NSFileManager *manager = [NSFileManager defaultManager];
- NSString *str = @"无线互联";
- NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
-
- BOOL sucess = [manager createFileAtPath:filePath contents:data attributes:nil];
- if(sucess){
- NSLog(@"文件创建成功");
- }else{
- NSLog(@"文件创建失败");
- }
NSFileManager内部使用了单例模式,这个后面会说到OC中得单例模式,然后就是构建一个NSData类,最后使用createFileAtPath方法创建文件,不过最后的attributes参数一般是用来给这个文件定义一些属性的,当然文件本身也是有很多默认的属性,比如文件的大小,文件的创建日期等,我们也可以自己在追加一些属性。
2、创建文件夹
-
- NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/file"];
- NSError *error;
-
- BOOL success1 = [manager createDirectoryAtPath:filePaths withIntermediateDirectories:YES attributes:nil error:&error];
- if(!success1){
- NSLog(@"创建成功");
- }else{
- NSLog(@"创建失败");
- }
从这两个操作看出来,他和Java中的File类很相似的
3、读取文件
-
-
- NSData *datas = [manager contentsAtPath:filePath];
- NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"%@",s);
4、剪切文件
-
-
-
- NSString *targetPath = [homePath stringByAppendingPathComponent:@"Documents/file/file2.text"];
- BOOL sucess2 = [manager moveItemAtPath:filePath toPath:targetPath error:nil];
- if(sucess2) {
- NSLog(@"移动成功");
- }else{
- NSLog(@"移动失败");
- }
这里有两个参数:一个是需要移动文件的路径,和需要移动到哪的路径
5、复制文件
-
- BOOL sucess3 = [manager copyItemAtPath:filePath toPath:targetPath error:nil];
- if(sucess3){
-
- }else{
-
- }
6、删除文件
-
-
- BOOL isExist = [manager fileExistsAtPath:filePath];
- if(isExist){
- BOOL sucess4 = [manager removeItemAtPath:filePath error:nil];
- if(sucess4){
-
- }else{
-
- }
- }
7、操作文件的属性
-
- NSDictionary *dic = [manager attributesOfItemAtPath:filePath error:nil];
- NSLog(@"%@",dic);
属性一般是NSDictionary
二、NSFileHandle
这个类主要是对文件进行读写操作的
看代码:
-
-
-
-
-
-
-
-
- #import <UIKit/UIKit.h>
- #import "AppDelegate.h"
-
-
-
-
-
-
-
-
- int main(int argc, charchar * argv[]) {
- @autoreleasepool {
-
-
- NSString *str = @"无线互联";
- NSString *homePath = NSHomeDirectory();
- NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
-
-
-
-
-
- BOOL sucess = [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- if(sucess){
-
- }else{
-
- }
-
-
- NSString *str1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
-
-
- NSLog(@"%@",str1);
-
-
-
-
- NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
-
- NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"%@",s);
-
-
- BOOL sucess1 = [data writeToFile:filePath atomically:YES];
- if(sucess1){
-
- }else{
-
- }
-
-
-
- NSArray *array = @[@"zhangsan",@"lisi"];
-
- NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/array.plist"];
- BOOL sucess2 = [array writeToFile:filePaths atomically:YES];
- if(sucess2){
-
- }else{
-
- }
-
- NSArray *arrays = [[NSArray alloc] initWithContentsOfFile:filePath];
- NSLog(@"%@",arrays);
-
-
-
- NSDictionary *dic = @{@"zhang":@"zhangsan",@"li":@"lisi"};
- BOOL sucess3 = [dic writeToFile:filePath atomically:YES];
- if(sucess3){
-
- }else{
-
- }
-
-
- dic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
- NSLog(@"%@",dic);
-
-
- return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
- }
- }
这个类就和Java中的FileInputStream/FileOutputStream类似了
其实我们在之前看到归档和解档操作的时候,那就是相当于写文件和读文件操作了。
1、字符串直接写入文件
-
- NSString *str = @"无线互联";
- NSString *homePath = NSHomeDirectory();
- NSString *filePath = [homePath stringByAppendingPathComponent:@"Documents/file.text"];
-
-
-
-
-
- BOOL sucess = [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
- if(sucess){
-
- }else{
-
- }
-
-
- NSString *str1 = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
-
-
- NSLog(@"%@",str1);
我们使用拼接的方式创建Documents目录中的file.text文件,然后将字符串内容写入到其中。
writeToFile方法进行写入
其实OC中的各种数据结构都有这个方法的:NSString,NSData,NSArray,NSSet,NSDirctionary等
这里有两点要注意的:
1)
数组只能将如下类型写入文件,如果包含其他对象,将写入失败:NSNumber,NSString,NSData,NSDate,NSArray,NSDictionary
数组、字典写入的文件叫做属性文件,可以使用xcode进行编辑
这个就是系统提供的一个属性文件:info.plist
这个和Java中的Property文件很类似
2)writeToFile方法的第一个参数:automically
现在有这样的场景,第一次把字符串写入到文件中,当我们修改字符串之后,再次写入的时候,但是可能会写入失败但是之前的内容也有可能丢失,因为每次在写入新的内容的时候,会剪切之前的内容,所以这里就有可能新的没有写成功,旧的文件也丢失了所以这时候atomically参数:YES:会将新内容先写入到一个缓存文件中,如果写入缓存成功之后,这时候就将这个缓存文件替换旧文件,这样就很安全了
2、NSData的内容读写
-
-
- NSData *data = [[NSData alloc] initWithContentsOfFile:filePath];
-
- NSString *s = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
- NSLog(@"%@",s);
-
-
- BOOL sucess1 = [data writeToFile:filePath atomically:YES];
- if(sucess1){
-
- }else{
-
- }
3、NSArray的内容读写
-
- NSArray *array = @[@"zhangsan",@"lisi"];
-
- NSString *filePaths = [homePath stringByAppendingPathComponent:@"Documents/array.plist"];
- BOOL sucess2 = [array writeToFile:filePaths atomically:YES];
- if(sucess2){
-
- }else{
-
- }
-
- NSArray *arrays = [[NSArray alloc] initWithContentsOfFile:filePath];
- NSLog(@"%@",arrays);
4、NSDirctionary的内容读写
-
- NSDictionary *dic = @{@"zhang":@"zhangsan",@"li":@"lisi"};
- BOOL sucess3 = [dic writeToFile:filePath atomically:YES];
- if(sucess3){
-
- }else{
-
- }
-
-
- dic = [[NSDictionary alloc] initWithContentsOfFile:filePath];
- NSLog(@"%@",dic);
上面说到的系统的info.plist文件就是NSDirctionary
三、在来看一下NSFileHandle的其他用法
-
-
-
-
-
-
-
-
- #import <UIKit/UIKit.h>
- #import "AppDelegate.h"
-
-
- int main(int argc, charchar * argv[]) {
- @autoreleasepool {
-
-
- NSString *str = @"无线互联";
- NSString *homePath = NSHomeDirectory();
- NSString *filePath = [homePath stringByAppendingPathComponent:@"file.test"];
- [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
-
- NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
-
-
- [handle seekToEndOfFile];
- NSString *s = @"123";
- NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];
- [handle writeData:data];
-
- [handle closeFile];
-
-
-
- NSFileHandle *handles = [NSFileHandle fileHandleForReadingAtPath:filePath];
-
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSDictionary *fileAttr = [fileManager attributesOfItemAtPath:filePath error:nil];
- NSNumber *fileSize = [fileAttr objectForKey:NSFileSize];
- long long sizeValue = [fileSize longLongValue];
-
-
- [handle seekToFileOffset:sizeValue/2];
-
- NSData *datas = [handle readDataToEndOfFile];
- NSString *s2 = [[NSString alloc] initWithData:datas encoding:NSUTF8StringEncoding];
- NSLog(@"%@",s2);
-
-
-
-
-
-
- NSString *targetPath = [homePath stringByAppendingPathComponent:@"files.test"];
- NSFileManager *fileManagers = [NSFileManager defaultManager];
- [fileManagers createFileAtPath:targetPath contents:nil attributes:nil];
-
-
- NSFileHandle *readHandles = [NSFileHandle fileHandleForReadingAtPath:filePath];
-
- NSFileHandle *writeHandles = [NSFileHandle fileHandleForWritingAtPath:targetPath];
-
-
- NSData *datass = [readHandles readDataToEndOfFile];
-
-
- [writeHandles writeData:datass];
-
-
- [readHandles closeFile];
- [writeHandles closeFile];
-
-
-
-
-
-
- return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
- }
- }
1、对原有的内容进行追加操作
-
- NSString *str = @"无线互联";
- NSString *homePath = NSHomeDirectory();
- NSString *filePath = [homePath stringByAppendingPathComponent:@"file.test"];
- [str writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
-
- NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
-
-
- [handle seekToEndOfFile];
- NSString *s = @"123";
- NSData *data = [s dataUsingEncoding:NSUTF8StringEncoding];
- [handle writeData:data];
-
- [handle closeFile];
记得操作完之后,关闭文件,我们在追加文件的时候,只要在将当前游标移动到文件的末尾处即可,默认是在开始处
2、读取文件的任何位置内容
-
- NSFileHandle *handles = [NSFileHandle fileHandleForReadingAtPath:filePath];
-
-
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSDictionary *fileAttr = [fileManager attributesOfItemAtPath:filePath error:nil];
- NSNumber *fileSize = [fileAttr objectForKey:NSFileSize];
- long long sizeValue = [fileSize longLongValue];
-
-
- [handle seekToFileOffset:sizeValue/2];
-
- NSData *datas = [handle readDataToEndOfFile];
- NSString *s2 = [[NSString alloc] initWithData:datas encoding:NSUTF8StringEncoding];
- NSLog(@"%@",s2);
还是设置一下游标的位置即可
3、实现文件的复制功能
-
-
- NSString *targetPath = [homePath stringByAppendingPathComponent:@"files.test"];
- NSFileManager *fileManagers = [NSFileManager defaultManager];
- [fileManagers createFileAtPath:targetPath contents:nil attributes:nil];
-
-
- NSFileHandle *readHandles = [NSFileHandle fileHandleForReadingAtPath:filePath];
-
- NSFileHandle *writeHandles = [NSFileHandle fileHandleForWritingAtPath:targetPath];
-
-
- NSData *datass = [readHandles readDataToEndOfFile];
-
-
- [writeHandles writeData:datass];
-
-
- [readHandles closeFile];
- [writeHandles closeFile];
-
-