iOS文件处理介绍(一)
一、在Documents、tmp和Library中存储文件
Documents:用于存储应用程序中经常需要读取或写入的常规文件。
tmp:用于存储应用程序运行时生成的文件。(随着应用程序的关闭失去了利用价值)
Library:一般存放应用程序的配置文件,比如说plist类型的文件。
二、读取和写入文件
1、新建Empty Application应用程序,添加HomeViewController文件。
HomeViewController.h代码:
| 1 | #import <UIKit/UIKit.h> |
| 2 | |
| 3 | @interface HomeViewController : UIViewController |
| 4 | { |
| 5 | |
| 6 | } |
| 7 | - (NSString *) documentsPath;//负责获取Documents文件夹的位置 |
| 8 | - (NSString *) readFromFile:(NSString *)filepath; //读取文件内容 |
| 9 | - (void) writeToFile:(NSString *)text withFileName:(NSString *)filePath;//将内容写到指定的文件 |
| 10 | @end |
HomeViewController.m代码:
| 1 | #import "HomeViewController.h" |
| 2 | @interface HomeViewController () |
| 3 | @end |
| 4 | @implementation HomeViewController |
| 5 | //负责获取Documents文件夹的位置 |
| 6 | - (NSString *) documentsPath{ |
| 7 | NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); |
| 8 | NSString *documentsdir = [paths objectAtIndex:0]; |
| 9 | return documentsdir; |
| 10 | } |
| 11 | |
| 12 | |
| 13 | //读取文件内容 |
| 14 | - (NSString *) readFromFile:(NSString *)filepath{ |
| 15 | if ([[NSFileManager defaultManager] fileExistsAtPath:filepath]){ |
| 16 | NSArray *content = [[NSArray alloc] initWithContentsOfFile:filepath]; |
| 17 | NSString *data = [[NSString alloc] initWithFormat:@"%@", [content objectAtIndex:0]]; |
| 18 | [content release]; |
| 19 | return data; |
| 20 | } else { |
| 21 | return nil; |
| 22 | } |
| 23 | } |
| 24 | //将内容写到指定的文件 |
| 25 | - (void) writeToFile:(NSString *)text withFileName:(NSString *)filePath{ |
| 26 | NSMutableArray *array = [[NSMutableArray alloc] init]; |
| 27 | [array addObject:text]; |
| 28 | [array writeToFile:filePath atomically:YES]; |
| 29 | [array release]; |
| 30 | } |
| 31 | |
| 32 | |
| 33 | -(NSString *)tempPath{ |
| 34 | return NSTemporaryDirectory(); |
| 35 | } |
| 36 | - (void)viewDidLoad |
| 37 | { |
| 38 | NSString *fileName = [[self documentsPath] stringByAppendingPathComponent:@"content.txt"]; |
| 39 | |
| 40 | //NSString *fileName = [[self tempPath] stringByAppendingPathComponent:@"content.txt"]; |
| 41 | |
| 42 | [self writeToFile:@"苹果的魅力!" withFileName:fileName]; |
| 43 | |
| 44 | NSString *fileContent = [self readFromFile:fileName]; |
| 45 | |
| 46 | NSLog(fileContent); |
| 47 | |
| 48 | [super viewDidLoad]; |
| 49 | } |
| 50 | @end |
效果图:


本文转载至 http://mobile.9sssd.com/ios/art/953
iOS文件处理介绍(一)的更多相关文章
- iOS Simulator功能介绍关于Xamarin IOS开发
iOS Simulator功能介绍关于Xamarin IOS开发 iOS Simulator功能介绍 在图1.38所示的运行效果中,所见到的类似于手机的模型就是iOS Simulator.在没有iPh ...
- ios中框架介绍
ios中框架介绍 参考博客: 参考文章:框架介绍 框架介绍 框架就是一个目录,一个目录包含了共享库,访问共享库里面的代码的头文件,和其他的图片和声音的资源文件.一个共享库定义的方法和函数可以被应用程序 ...
- 【整理】Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得
[整理]Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得 iOS模拟器简介 iOS功能简介 iOS模拟器,是在Mac下面开发程序时,开发iOS平台的程序时候,可以使用的辅助工具 ...
- IOS自动化打包介绍
IOS自动化打包介绍 标签: app打包 , Ios打包 , iphone打包 , iphone自动化打渠道包 分类:无线客户端技术, 贴吧技术 摘要 随着苹果手持设备用户的不断增加,ios应 ...
- Unity——资源文件夹介绍
Unity资源文件夹介绍 1.编辑时 在Asset文件下存在Resources和SteamingAsset文件夹: Resources 只读不可修改,打包时直接写死,没有办法通过热更新替换资源: 可以 ...
- TeamViewer12.0.71503(远程控制软件)精简版单文件企业版介绍
TeamViewer 是一款能在任何防火墙和 NAT 代理的后台用于远程控制,桌面共享和文件传输的简单且快速的解决方案.为了连接到另一台计算机,只需要在两台计算机上同时运行 TeamViewer 即可 ...
- OC多文件开发介绍
OC多文件开发介绍: 1.为什么要使用多文件? 在工作中,通常把不同的类放到不同的文件中,每个类的声明和实现分开,声明写在.h头文件中,实现写在相应的.m文件中去,类名是什么,文件名的前缀就是什么.假 ...
- iOS: 属性列表介绍 Introduction to Property Lists
iOS: 属性列表介绍 Introduction to Property Lists 从本质上说, 属性列表就是苹果的对象数据序列化与反序列化方式 属性列表使用几种数据类型把数据组织为键值表和值表 P ...
- iOS——文件操作NSFileManager (创建、删除,复制,粘贴)
iOS——文件操作NSFileManager (创建.删除,复制,粘贴) iOS的沙盒机制,应用只能访问自己应用目录下的文件.iOS不像android,没有SD卡概念,不能直接访问图像.视 ...
随机推荐
- JavaScript高级 面向对象(2)--调试工具的使用:音乐循环播放
说明(2017.3.29): 1. 在调试工具console里输入var v = document.createElement("audio"),然后再source的watch里输 ...
- 基于CSS3自定义发光radiobox单选框
之前我们分享过一些CSS3和HTML5实现的自定义checkbox和Radiobox,比如纯CSS3美化Checkbox和Radiobox按钮,不仅外观唯美,而且Radiobox选中时还有动画效果.今 ...
- [systemd]How To Use Systemctl to Manage Systemd Services and Units
转自: https://www.digitalocean.com/community/tutorials/how-to-use-systemctl-to-manage-systemd-services ...
- 怎么清除Win10运行中的使用记录,不记录win10的运行记录
点击“开始”把上两项关闭.
- SpringAOP来监控service层中每个方法的执行时间
使用AOP来说,太方便了,并且特别适合这类场景. 代码如下,这里是将要统计的信息写到log文件中,也可以设计成写入表中. package com.ecsoft.interceptor; import ...
- Drupal模块的安装方法
Drupal自身的安装很简单,新建一个数据库,然后根据安装提示一步一步做就OK了. 而Drupal可以通过安装各种模块来提供更多定制功能,这些模块的安装方法基本相同,一般来说,就是以下几步: 1) 从 ...
- cat /etc/init.d/nfs 这句话看不懂
if status rpc.mountd > /dev/null ; then exit 0 fi Linux非root用户程序使用小于1024端口 在Linux下,默认端口1024下的是 ...
- ERROR 1290
Mysql创建用户时.出现如下错误! mysql> create user 'testuse'@'localhost' identified by '111111'; ERROR 1290 (H ...
- yuv420格式分析
http://blog.csdn.net/liuhongxiangm/article/details/9135791 http://blog.csdn.net/bluesky_sunshine/art ...
- 使用_snscanf_s转换十六进制时引起的内存越界
//将Hex编码转换为指定编码格式的字符串 string Encoding::DecodeHexString(const string &strSrc, UINT code_page ) { ...