NSFileManager(文件管理器)
对文件本身的的操作,创建文件、删除文件、移动、拷贝和遍历以及对目录操作等方面,需注意它并没有打开一个文件去修改里面的内容。
NSFileHandle(文件句柄)
是对文件内容的操作,文件句柄打开一个文件会自动生成;之后,这个文件句柄就成了文件的代表,对文件句柄的操作就是对文件操作。
-----NSFileManager的操作------
创建一个单例的NSFileManager对象
NSFileManager *fm = [NSFileManager defaultManager];
文件操作方法:
1.从文件中读取数据
BOOL isOk = [fm contentsAtPath:path];
BOOL isOk = [fm createFileAtPath:(NSString*) contents:(NSData*) attributes:(NSDictionary*)];
[fm removeItemAtPath:(NSString*) error:(NSError**)];
BOOL isOk = [fm removeFileAtPath:path handler:handler];
BOOL isOk = [fm copyItemAtPath:(NSString*) toPath:(NSString*) error:(NSError**)];
BOOL isOk = [fm moveItemAtPath:(NSString*) toPath:(NSString*) error:(NSError**)];
BOOL isOk = [fm contentsEqualAtPath:path1 andPath:path2];
BOOL isOk = [fm fileExistsAtPath:path];
BOOL isOk = [fm isReadablefileAtPath:path ];
BOOL isOk = [fm isWritablefileAtPath:path ];
NSDictionary *dict = [fm fileAttributesAtPath:path traverseLink:(BOOL)flag];
BOOL isOk = [fm changeFileAttributes:attr atPath:path
目录操作方法:
BOOL isOk = [fm createDirectoryAtPath:(NSString*)
NSString *str = [fm currentDirectoryPath];
BOOL isOk = [fm changeCurrentDirectoryPa
BOOL isOk = [fm copyPath:from toPath:path tohandler:handler];
BOOL isOk = [fm fileExistsAtPath:path isDirectory:(BOOL*)flag];
NSArray *array = [fm contentsOfDirectoryAtPat
NSDirectoryEnumerator *enumerator =[fm enumeratorAtPath:path];
BOOL isOk = [fm removeFileAtPath:path handler:handler];
BOOL isOk = [fm movePath:from toPath:path tohandler:handler];
NSArray *array = [fm contentsOfDirectoryAtPat
if(error){
}
NSLog(@"%@",array);
NSArray *arrayOne = [fm subpathsOfDrictoryAtPath
-----NSFileHandle的操作------
1.创建文件句柄
NSFileHandle *fhRead = [NSFileHandle fileHandleForReadingAtPa
NSFileHandle *fhWrite = [NSFileHandle fileHandleForWritingAtPa
NSFileHandle *fhUpdate = [NSFileHandle fileHandleForUpdatingAtP
2.读操作
NSData *data = [fhRead readDataOfLength:3];
NSData *data = [fhRead readDataToEndOfFile];
NSData *data = [fhRead availableData];
3.写操作
[fhWrite writeData:(NSData*)];
[fhWrite truncateFileAtOffSet:0];
id
[fhWrite seekToFileOffset:offset];
id theEnd = [fhWrite seekToEndFile];
[fhWrite truncateFileAtOffset:offset];
4.关闭文件
[fhRead closeFile];
[fhWrite closeFile];
[fhUpdate closeFile];
练习例子:
NSString *strFile = @" thefile.txt ";
NSString *strTextCreate = @"创建成功?";
NSFileManager *fm;
NSString *strPath = [NSHomeDirectory() stringByAppendingPathComponent:strFile];
NSData *dataTextCreate = [strTextCreate dataUsingEncoding:NSUTF8StringEncoding];
NSData *dataReadOne = [fm contentsAtPath:strPath];
NSString *strReadOne = [[NSString alloc] initWithData:dataReadOne encoding:NSUTF8StringEncoding];
NSLog(@"\n the new file include text:\n",strReadOne);
NSFileHandle *fhWrite = [NSFileHandle fileHandleForWritingAtPa
NSString *strAdd = @"添加成功?";
NSData *dataAdd = [strAdd dataUsingEncoding:strAddencoding:NSUTF8StringEncoding];
[fhWrite seekToEndOfFile];
[fhWrite writeData:dataAdd];
[fhWrite closeFile];
NSFileHandle *fhRead = [NSFileHandle fileHandleForReadingAtPa
NSData *dataReadTwo = [fhRead availableData];
NSString *strReadTwo = [[NSString alloc] initWithData:dataReadTwo encoding:NSUTF8StringEncoding];
NSLog(@"\n use NSFlieHandle method read:\n %@",strReadTwo);
[fhRead closeFile];