/*

*  @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 = @"我的测试数据";

/**

*

*  @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");

}

ios文件读取的更多相关文章

  1. ios文件读取(二)

    - (void)viewDidLoad { [super viewDidLoad]; /** *  @brief 获取文件路径 * */ NSString * filePath = [self get ...

  2. ios 文件操作(NSFileManager)

    IOS的沙盒机制,应用只能访问自己应用目录下的文件,iOS不像android,没有SD卡概念,不能直接访问图像.视频等内容. iOS应用产生的内容,如图像.文件.缓存内容等都必须存储在自己的沙盒内. ...

  3. [iOS翻译]《iOS 7 Programming Cookbook》:iOS文件与文件夹管理(上)

    简介: iOS基于OS X,而OSX本身基于Unix操作系统.在iOS里面,操作系统的完全路径结构是不可见的,因为每个APP的数据都存储自身的沙盒里面.沙盒环境实际上听起来像这样:一个只允许当前APP ...

  4. ios 文件操作

    1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...

  5. iOS——文件操作NSFileManager (创建、删除,复制,粘贴)

    iOS——文件操作NSFileManager (创建.删除,复制,粘贴)       iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...

  6. IOS文件操作的两种方式:NSFileManager操作和流操作

    1.常见的NSFileManager文件方法 -(NSData *)contentsAtPath:path //从一个文件读取数据 -(BOOL)createFileAtPath: path cont ...

  7. iOS文件处理类

    iOS文件处理类 这是一个用来简化iOS中关于文件操作的一个类,所有方法都为类方法. Source File.h // // File.h // FileManager // // http://ho ...

  8. java中的文件读取和文件写出:如何从一个文件中获取内容以及如何向一个文件中写入内容

    import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.Fi ...

  9. php xml 文件读取 XMLReader

    php xml 文件读取 <?php /** $xmlString = '<xml> <persons count="10"> <person ...

随机推荐

  1. [Leetcode][001] Two Sum (Java)

    题目在这里: https://leetcode.com/problems/two-sum/ [标签]Array; Hash Table [个人分析] 这个题目,我感觉也可以算是空间换时间的例子.如果是 ...

  2. Clover周报模块 -- 开发总结

    2014年7月8日 00:16:05 一.切图 这次开发,切图花了不少时间,样式是用scss写的,第一次用,不过用着用着就发现它的强大,层级.作用域.重用等都非常的方便,还有考拉神器,用着真是爽!不过 ...

  3. destoon系统商城加淘宝客按钮方法

    destoon系统很多喜欢运营B2B的站长都在用,其中的商城模块常常被用来做淘宝客,其中的难点是如何把购买按钮做成淘宝客地址,这个问题的修改在论坛上被叫价50元,下面小编把这个实用的方法分享下,希望对 ...

  4. Asp.Net 控件radio 的简单使用

    js: <script type="text/javascript"> function ok() { document.getElementById("tx ...

  5. C程序设计语言练习题1-10

    练习1-10 编写一个将输入复制到输出的程序,并将起重的制表符替换为\t,把回退符替换成\b,把反斜杠替换为\\.这样可以将制表符和回退符以可见的方式显示出来. 代码如下: #include < ...

  6. opencv 批量图像读写

    处理图像数据集时通常要读写整个文件夹里的图像,这时就会用的图像的批量读写. 比较常用的方法就是生成一个包含所有图像的txt列表 生成txt文件的方法如下: 利用cmd进入dos 利用路径进入指定文件夹 ...

  7. Spring MVC 和Struts2对比

    Spring MVC和Struts2的区别: 1. 机制:spring mvc的入口是servlet,而struts2是filter,这样就导致了二者的机制不同. ​2. 性能:spring会稍微比s ...

  8. IOS 动画专题 --iOS核心动画

    iOS开发系列--让你的应用“动”起来 --iOS核心动画 概览 通过核心动画创建基础动画.关键帧动画.动画组.转场动画,如何通过UIView的装饰方法对这些动画操作进行简化等.在今天的文章里您可以看 ...

  9. Qt程序Windows部署前打包方法

    1.需求 很多公司都在使用Qt作为GUI库,在开发者的计算机上通常是利用配置PATH环境变量来实现调用Qt的DLL.然而当把开发后的软件直接部署在顾客方,顾客的计算机上并没有Qt的bin目录,所以并不 ...

  10. selenium太有爱,我已离不开!!!

    自动化测试,超有用. PROXY,PLUGIN,PROFILE,WINDOWS HANDLE个个搞定!!! from selenium import webdriver from selenium.c ...