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卡概念,不能直接访问图像.视 ...
随机推荐
- 略过ftp利用samba共享
开发阶段每次通过ftp上传代码到服务器是件麻烦事,而且有时候一些小错误直接通过ftp在服务器上修改后,容易忘记在本地保存. 于是想到了网络共享,windows上直接挂载网络硬盘,这样就可以略过ftp了 ...
- js获取下拉,单选
1.JS取下拉框的显示值和判断单选按钮 1.需要得到select组件显示的值.下面是经常用到的方法: Html 源码: <html><body><select id=&q ...
- posix进程间通信
************************************************************************************************** p ...
- 轻量级ORM框架Dapper应用二:使用Dapper实现CURD操作
在上一篇文章中,讲解了如何安装Dapper,这篇文章中将会讲解如何使用Dapper使用CURD操作. 例子中使用到的实体类定义如下: using System; using System.Collec ...
- unicode编码转汉字
String hexB = Integer.toHexString(utfBytes[byteIndex]); //转换为16进制整型字符串 if (hexB.length() <= 2) ...
- windows 解压缩命令
首先安装winrar 压缩: 命令:start winrar a test test.py 解压: 命令:start winrar x -y test.rar F:\batShell\test\tes ...
- 谷歌修复了 FFmpeg 中上千个 bug
谷歌在科技业界中几乎每天都会创造出新闻素材,它的触手涉及到了生活中的多个领域.最近谷歌将其Google +社交网络与邮件服务Gmail相结合.然而今天谷歌宣布他们修复了FFmpeg的上千个bug. ...
- erlang在NotePad++下的高亮
转自:http://www.roberthorvick.com/2009/07/08/syntax-highlighing-for-erlang-in-notepad/ Syntax Highligh ...
- 【Java面试题】39 Set里的元素是不能重复的,那么用什么方法来区分重复与否呢? 是用==还是equals()? 它们有何区别?
1.什么是Set?(what) Set是Collection容器的一个子接口,它不允许出现重复元素,当然也只允许有一个null对象. 2.如何来区分重复与否呢?(how) “ 用 iterator() ...
- jquery日期插件datePicker
index.html <!DOCTYPE html> <html lang="zh-cn"> <head> <meta http-equi ...