//1、文件夹、文件的创建和删除
NSFileManager *fileManager=[NSFileManager defaultManager];
NSString *filePath=@"/Users/apple/Desktop/";
NSError *error;
//判断当前文件夹是否存在
BOOL isExist=[fileManager contentsOfDirectoryAtPath:[NSString stringWithFormat:@"%@",filePath] error:&error];
//创建子文件夹
BOOL isExist1=[fileManager createDirectoryAtPath:[NSString stringWithFormat:@"%@/hello"] withIntermediateDirectories:YES attributes:nil error:&error];
//在指定路径下创建文件内容
BOOL isExist2=[fileManager createFileAtPath:[NSString stringWithFormat:@"%@/hello/temp"] contents:[@"hello world" dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
BOOL isExist3=[fileManager removeItemAtPath:[NSString stringWithFormat:@"%@/hello"] error:&error];
//2、文件的读取和写入
NSFileHandle *handle=[NSFileHandle fileHandleForReadingAtPath:[NSString stringWithFormat:@"%@/hello",filePath]];
//读取文件的内容
NSData *fileData=[handle readDataToEndOfFile];
NSString *fileStr=[[NSString alloc]initWithData:fileData encoding:NSUTF8StringEncoding];
//文件的读取
NSFileHandle *fh=[NSFileHandle fileHandleForWritingAtPath:[NSString stringWithFormat:@"%@/hello",filePath]];
NSData *writeData=[@"hello" dataUsingEncoding:NSUTF8StringEncoding];
//定位到文件最后
[fh seekToEndOfFile];
//写入文件
[fh writeData:writeData];
网友评论