因为自己总是忘记这几条语句,所以保存起来以免下次再翻以前的代码,注释写的很清楚,直接上代码了呦~
//
// ViewController.m
// fileManagerExe
//
// Created by a111 on 16/4/8.
// Copyright © 2016年 司小文. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//找到相应的目录
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//目录下的文件夹名字'aaa'
NSString *pathFile = [path stringByAppendingPathComponent:@"aaa"];
//判断有没有文件夹
BOOL isDir =NO;
NSFileManager *fileManager = [NSFileManager defaultManager];
BOOL existed = [fileManager fileExistsAtPath:pathFile isDirectory:&isDir];
if ( !(isDir ==YES && existed == YES) ){
//如果没有文件夹则创建
[fileManager createDirectoryAtPath:pathFile withIntermediateDirectories:YES attributes:nil error:nil];
}else{
//删除文件夹
//[fileManager removeItemAtPath:pathFile error:nil];
}
//在目录文件夹下找相应文件'bbb.plist'
NSString *filename=[pathFile stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.plist",@"bbb"]];
if (![fileManager fileExistsAtPath:filename]) {
//如果没有文件,添加新内容,生成新文件
NSDictionary *dic = @{@"aaa":@"1"};//(内容随便写的)
[dic writeToFile:filename atomically:YES];
}else{
//如果有文件,获取文件夹文件
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filename];
NSLog(@"%@",dic);
}
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
感谢观看,学以致用更感谢。
本文介绍了如何使用Objective-C在iOS应用中进行文件管理和数据持久化操作,包括查找指定目录、创建和删除文件夹以及读写文件的具体实现。

2332

被折叠的 条评论
为什么被折叠?



