/*

*  @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. window对象BOM

    BOM的和新对象是window,他表示流浪器的一个实例,作为一个Global对象,有权访问parseInt()等方法 在全局作用域声明的变量,函数都有钱访问 ; function sayName () ...

  2. 如何用正则将多个空格看成一个空格结合spllit()方法将文本数据入库

    关键的代码和正则表达式在这里 while((line=br.readLine())!=null) { String[] data=new String[4]; data=line.split(&quo ...

  3. Http服务器性能测试工具ab..

    -A auth-username:password 对服务器提供BASIC认证信任.用户名和密码由一个:隔开,并以base64编码形式发送,无论服务器是否需要(即,是否发送了401认证需求代码),此字 ...

  4. 数学函数类方法的使用.java

    public class Test { public static void main(String[] args) { double a=2,b=3; double z1=Math.pow(a,b) ...

  5. mysql windows 下导入大文件

    先进入你的mysql bin目录 cd D:/php/mysql/bin 输入命令 mysql -u 用户名 -p 密码 数据库名 < 文件路径                          ...

  6. android:showAsAction="never"是做什么用的?

    安卓开发项目文件中有一个目录叫做menu,里面有tybmain.xmlitem选项里有一句 android:showAsAction = "never"那么这句话是做什么用的呢?原 ...

  7. Codeforces 566F Clique in the Divisibility Graph

    http://codeforces.com/problemset/problem/566/F 题目大意: 有n个点,点上有值a[i], 任意两点(i, j)有无向边相连当且仅当 (a[i] mod a ...

  8. 用Altium designer画PCB的一般心得

    一.电路版设计的先期工作 1.利用原理图设计工具绘制原理图,并且生成对应的网络表.当然,有些特殊情况下,如电路版比较简单,已经有了网络表等情况下也可以不进行原理图的设计,直接进入PCB设计系统,在PC ...

  9. qt鼠标事件总结(坐标,跟踪,点击判断)

    1.QMouseEvent中的坐标QMouseEvent中保存了两个坐标,一个是全局坐标,当然另外一个是局部坐标.全局坐标(globalPos())即是桌面屏幕坐标(screen coordinate ...

  10. ECMAScript 5/6/7兼容性速查表

    http://kangax.github.io/compat-table/es5/ 秒查ECMAScript在各大浏览器的兼容性,点击右上角按钮可以“在5/6/7/非标”之间切换.做JavaScrip ...