#pragma mark 文件是否存在
NSFileManager *fileManager=[NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:filePath]) {}
#pragma mark 获取沙盒路径
NSString *path=NSHomeDirectory();
#pragma mark 获取Documents路径
NSArray *docPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path=[docPaths lastObject];
#pragma mark 获取Library路径
NSArray *docPaths=NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *path=[docPaths lastObject];
#pragma mark 获取tmp路径
NSString *path=NSTemporaryDirectory();
#pragma mark 在Documents里面创建一个“测试”的文件夹
NSString *path=[self getDocumentsPath];
path=[path stringByAppendingPathComponent:@"测试"];
NSFileManager *fileManager=[NSFileManager defaultManager];
BOOL ret=[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
if (ret) {
NSLog(@"文件夹创建成功");
}else{
NSLog(@"文件夹创建失败");
}
}
#pragma mark 在“测试”的文件夹创建一个“测试.txt”的文件并写入“哈哈哈哈哈哈哈哈”
-(void)writeFile{
NSString *path=[self getDocumentsPath];
path=[path stringByAppendingPathComponent:@"测试"];
if ([self isFile:path]) {
path=[path stringByAppendingPathComponent:@"测试.txt"];
NSString *content=@"哈哈哈哈哈哈哈哈";
BOOL ret=[content writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (ret) {
NSLog(@"文件写入成功");
}else{
NSLog(@"文件写入失败");
}
}else{
NSFileManager *fileManager=[NSFileManager defaultManager];
BOOL ret=[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
if (ret) {
NSLog(@"文件夹创建成功");
[self writeFile];
}else{
NSLog(@"文件夹创建失败");
}
}
}
#pragma mark 在“测试.txt”的文件续写入“啊啊啊啊啊啊啊”
-(void)addMoreFile{
NSString *path=[self getDocumentsPath];
path=[path stringByAppendingPathComponent:@"测试"];
path=[path stringByAppendingPathComponent:@"测试.txt"];
if ([self isFile:path]) {
//打开文件
NSFileHandle *fileHandle=[NSFileHandle fileHandleForUpdatingAtPath:path];
[fileHandle seekToEndOfFile];
NSString *content=@"啊啊啊啊啊啊啊";
NSData *data=[content dataUsingEncoding:NSUTF8StringEncoding];
[fileHandle writeData:data];
//关闭文件
[fileHandle closeFile];
}else{
NSLog(@"文件不存在");
}
}
#pragma mark 删除文件"测试.txt"
-(void)deleteFile{
NSString *path=[self getDocumentsPath];
path=[path stringByAppendingPathComponent:@"测试"];
path=[path stringByAppendingPathComponent:@"测试.txt"];
if ([self isFile:path]) {
NSFileManager *fileManager=[NSFileManager defaultManager];
BOOL ret=[fileManager removeItemAtPath:path error:nil];
if (ret) {
NSLog(@"文件删除成功");
}else{
NSLog(@"文件删除失败");
}
}else{
NSLog(@"文件不存在");
}
}
#pragma mark 实例在Documents创建一个image的文件夹,并将图片存进去
-(void)writeImage{
NSString *path=[self getDocumentsPath];
path=[path stringByAppendingPathComponent:@"image"];
NSFileManager *fileManager=[NSFileManager defaultManager];
BOOL ret=[fileManager createDirectoryAtPath:path withIntermediateDirectories:YES attributes:nil error:nil];
if (ret) {
UIImage *image=[UIImage imageNamed:@"IMG_0641.JPG"];
NSData *imageData=UIImageJPEGRepresentation(image, 1);
path=[path stringByAppendingPathComponent:@"IMG_0641.JPG"];
BOOL ret=[imageData writeToFile:path atomically:YES];
if (ret) {
NSLog(@"图片存入成功");
}else{
NSLog(@"图片存入失败");
}
}else{
NSLog(@"文件夹创建失败");
}
}
网友评论