标签:
/*
* @brief 沙盒下的各种文件
*/
- (void)getPaths
{
/**
* @brief 获取沙盒的路径
*/
NSString * HomeDirectory = NSHomeDirectory();
NSLog(@"%@",HomeDirectory);
/*返回值是数组的原因:该方法一开始是用于mac-os开发,对PC端来说,可以有多个用户,所以获取时,会获取到所有用户的文件夹路径,但是该方法用于ios开发时,因为移动端只用一个用户,所以获取到的路径也只有一个。*/
/**
* @brief 获取Documents目录路径 苹果建议将程序创建产生的文件以及应用浏览产生的文件数据保存在该目录下 iTunes备份和恢复的时候会包括此目录
*/
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentDirectory = [paths firstObject];
NSLog(@"%@",documentDirectory);
/**
* @brief 获取Library目录路径 保存程序的默认设置或其他状态信息. Library下的Preferences:存储偏好设置。比如:应用是否是第一次启动,保存用户名和密码等。
*/
NSArray * paths_1 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString * libraryDirectory = [paths_1 firstObject];
NSLog(@"%@",libraryDirectory);
/**
* @brief 获取Cache目录路径(Library下的文件) 存放缓存文件,保存应用的持久化数据,用于应用的升级或者应用关闭后的数据保存,不会被itunes同步。
*/
NSArray * path_2 = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString * cachePath = [path_2 firstObject];
NSLog(@"%@",cachePath);
/**
* @brief 获取tmp目录路径 提供一个临时创建的文件,不需要持久化,在应用关闭后,该目录下的数据将删除,页可能系统在程序不运行的时候清楚。
*/
NSString * tmpDirectory = NSTemporaryDirectory();
NSLog(@"%@",tmpDirectory);
}
/*
* @brief 文件的操作
*/
- (void)fileManager
{
/**
* @brief 创建
*/
[self createFile];
/**
* @brief 写入
*/
[self WriteToFile];
/**
* @brief 读取
*/
[self readFile];
/**
* @brief 删除
*/
[self deleteFile];
}
- (void)createFile
{
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths firstObject];
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * testDirectory = [documentsDirectory stringByAppendingPathComponent:@"MyTest"];
/**
* @brief 创建文件夹
*/
BOOL res = [fileManager createDirectoryAtPath:testDirectory withIntermediateDirectories:YES attributes:nil error:nil];
if (res) {
/**
* @brief 创建文件
*/
NSString * testPath = [testDirectory stringByAppendingPathComponent:@"test.text"];
NSLog(@"%@",testPath);
NSString * content = @"我的测试数据";
/**
* @author 时峰林
*
* @brief 写入数据
*/
BOOL res = [content writeToFile:testPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
if (res) {
NSLog(@"写入数据成功");
}
}
}
- (void)WriteToFile
{
NSFileManager * defaultManager = [NSFileManager defaultManager];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths firstObject];
NSString * testDirectory = [documentsDirectory stringByAppendingPathComponent:@"MyTest"];
NSString * testPath = [testDirectory stringByAppendingPathComponent:@"test.text"];
NSString * content = @"我的第二次测试";
/**
* @brief 文件的可写性(isWritableFileAtPath)
*/
if ([defaultManager isWritableFileAtPath:testPath]) {
/**
* @brief 写入数据
*/
if ([content writeToFile:testPath atomically:NO encoding:NSUTF8StringEncoding error:nil]) {
NSLog(@"写入成功");
}
}
}
- (void)readFile
{
NSFileManager * defaultManager = [NSFileManager defaultManager];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths firstObject];
NSString * testDirectory = [documentsDirectory stringByAppendingPathComponent:@"MyTest"];
NSString * testPath = [testDirectory stringByAppendingPathComponent:@"test.text"];
/**
* @brief 查询文件的可读性
*/
if ([defaultManager isReadableFileAtPath:testPath]) {
NSString * text = [NSString stringWithContentsOfFile:testPath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"%@",text);
}
}
- (void)deleteFile
{
NSFileManager * defaultManager = [NSFileManager defaultManager];
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectory = [paths firstObject];
NSString * testDirectory = [documentsDirectory stringByAppendingPathComponent:@"MyTest"];
NSLog(@"%@",testDirectory);
/**
* @brief 查询文件的可执行性(isExecutableFileAtPath)
*/
NSLog(@"%@",[defaultManager isExecutableFileAtPath:testDirectory]?@"YES":@"NO");
/**
* @brief 文件的可删除性(isDeletableFileAtPath)
*/
if ([defaultManager isDeletableFileAtPath:testDirectory]) {
/**
* @brief 删除指定路径的文件
*/
[defaultManager removeItemAtPath:testDirectory error:nil];
}
NSLog(@"%@",[defaultManager isExecutableFileAtPath:testDirectory]?@"YES":@"NO");
}
标签:
原文地址:http://www.cnblogs.com/shifenglin/p/5332641.html