oc文件基本读写及操作
代码:
#import <Foundation/Foundation.h> //NSString 写文件
void stringWriteToFile(){
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/test.txt"];
NSString *s = @"test";
[s writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil]; NSString *str = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"\nstring = %@",str); NSString *testtxt = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"\ntest.text = %@",testtxt);
} //NSArray 写文件
void arrayWriteToFile(){
NSArray *arr = @[@"a",@"b",@"b",@"c"];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/test2.txt"];
[arr writeToFile:path atomically:YES]; NSArray *a = [[NSArray alloc] initWithContentsOfFile:path];
NSLog(@"array = \n%@",a); NSString *s = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"\ntest2.txt = \n%@",s);
} //NSDictionary 写文件
void dictionaryWriteToFile(){
NSDictionary *dic = @{@"a":@"",
@"b":@"",
@"c":@"",
@"d":@""};
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/test3.txt"];
[dic writeToFile:path atomically:YES]; NSDictionary *d = [[NSDictionary alloc] initWithContentsOfFile:path];
NSLog(@"\ndictionary = \n%@",d); NSString *s = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSLog(@"\ntest3.txt = \n%@",s);
} //atomically 是否原子级 即事务性写入
//NSNumber、NSDate、NSData都可以通过writeToFile写入文件,该文件为纯文本类型,如果将后缀名改为plist即为xcode属性列表文件 int main(int argc, const char * argv[]) { //该方法是创建一个实例,但是使的NSFileManager的单例模式将失去效果
NSFileManager *fm0 = [[NSFileManager alloc] init]; //defaultManager使用单例模式创建NSFileManager对象
NSFileManager *fm1 = [NSFileManager defaultManager]; NSFileManager *fm2 = [NSFileManager defaultManager]; NSLog(@"\nfm0 = %p,fm1 = %p,fm2 = %p",fm0,fm1,fm2); NSData *data = [[NSString stringWithFormat:@"main"] dataUsingEncoding:NSUTF8StringEncoding]; NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/main.txt"]; if (![fm1 fileExistsAtPath:path]) {
if ([fm1 createFileAtPath:path contents: data attributes:nil]) {
NSLog(@"create success");
NSDictionary *d = [fm1 attributesOfItemAtPath:path error:nil]; NSLog(@"\nattributesOfItemAtPath = \n%@",d); //NSFileSize是预定义的文件属性key,可通过查看系统文件获取其他属性key并通过
//以下方法获取其属性值
NSNumber *filesize = [d valueForKey:NSFileSize]; NSLog(@"\nfilesize = %@",filesize); //读文件
NSData *data1 = [fm1 contentsAtPath:path]; NSString *s = [[NSString alloc] initWithData:data1 encoding:NSUTF8StringEncoding]; //文件系统的属性
//总空间,已用空间,可用空间,文件数量,
NSLog(@"\nattributesOfFileSystemForPath = \n%@",[fm1 attributesOfFileSystemForPath:path error:nil]); NSLog(@"\nmain.txt = %@",s);
}
}
else{ NSString *copypath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/main_copy.txt"];
[fm1 copyItemAtPath:path toPath:copypath error:nil]; //重命名可以目标路径与主路径一致但是文件名不同
NSString *movepath = [NSHomeDirectory() stringByAppendingPathComponent:@"/Documents/main_move.txt"];
[fm1 moveItemAtPath:path toPath:movepath error:nil]; //删除文件
if ([fm1 removeItemAtPath:path error:nil]) {
NSLog(@"remove %@ success",path);
}
if ([fm1 removeItemAtPath:copypath error:nil]) {
NSLog(@"remove %@ success",copypath);
}
if ([fm1 removeItemAtPath:movepath error:nil]) {
NSLog(@"remove %@ success",movepath);
}
} stringWriteToFile(); arrayWriteToFile(); dictionaryWriteToFile(); return ;
}
结果:
-- ::26.064 NSFileManagerDemo[:]
fm0 = 0x1001145d0,fm1 = 0x100114620,fm2 = 0x100114620
-- ::26.075 NSFileManagerDemo[:] remove /Users/yoran_yang/Documents/main_copy.txt success
-- ::26.076 NSFileManagerDemo[:] remove /Users/yoran_yang/Documents/main_move.txt success
-- ::26.076 NSFileManagerDemo[:]
string = test
-- ::26.077 NSFileManagerDemo[:]
test.text = test
-- ::26.078 NSFileManagerDemo[:] array =
(
a,
b,
b,
c
)
-- ::26.078 NSFileManagerDemo[:]
test2.txt =
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>a</string>
<string>b</string>
<string>b</string>
<string>c</string>
</array>
</plist>
-- ::26.079 NSFileManagerDemo[:]
dictionary =
{
a = ;
b = ;
c = ;
d = ;
}
-- ::26.079 NSFileManagerDemo[:]
test3.txt =
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>a</key>
<string></string>
<key>b</key>
<string></string>
<key>c</key>
<string></string>
<key>d</key>
<string></string>
</dict>
</plist>
Program ended with exit code:
oc文件基本读写及操作的更多相关文章
- java对excel文件内容读写修改操作
Read.java package domain; import java.io.FileInputStream; import java.io.InputStream; import jxl.Cel ...
- Linux下常用设置文件和文件夹读写权限操作
1.查看权限 ls -l xxx.xxx (xxx.xxx是文件名) 2.常见权限 -rw------- (600) 只有所有者才有读和写的权限 -rw-r--r-- (644) 只有所有者才有读 ...
- Android中对文件的读写进行操作
1. 在文件的地方生成一个read.txt文件,并且写入一个read数据.IO流用完之后一定要记得关闭. 对于try和catch是对于错误的抓取. 2. 首先先new file来找到那个文件,然后在通 ...
- OC文件操作(2)
NSFileManager 文件管理器完成文件的创建.移动.拷贝等管理操作 1.查询文件和目录 OC中查询路径下的目录主要分为浅度遍历和深度遍历. 浅度遍历 NSFileManager * ma ...
- 『无为则无心』Python基础 — 41、Python中文件的读写操作(一)
目录 1.文件操作步骤 2.文件的读写操作 (1)文件的打开 (2)打开文件模式 (3)获取一个文件对象 (4)关于文件路径 1.文件操作步骤 当我们要读取或者写入文件时,我们需要打开文件,在操作完毕 ...
- fopen()函数以"a+"方式打开一个不存在的文件后读写出现问题
问题:在完成课后习题的时候,使用fopen()函数以"a+"方式打开一个不存在的文件时,写入.读取出现错误: //添加用户输入单词后,在单词头加入编号,确保编号跟着前面的开始排序 ...
- INI 文件的读写操作
在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static r ...
- Android 对 properties文件的读写操作
-. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...
- C++学习48 对ASCII文件的读写操作
如果文件的每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件(或称字符文件).程序可以从ASCII文件中读入若干个字符,也可以向它输出一些字符. 对ASCI ...
随机推荐
- bzoj 1924 [Sdoi2010]所驼门王的宝藏(构图,SCC,DP)
Description Input 第一行给出三个正整数 N, R, C. 以下 N 行,每行给出一扇传送门的信息,包含三个正整数xi, yi, Ti,表示该传送门设在位于第 xi行第yi列的藏宝宫室 ...
- HDU 4195 Regular Convex Polygon
思路:三角形的圆心角可以整除(2*pi)/n #include<cstdio> #include<cstring> #include<iostream> #incl ...
- 改进的SMO算法
S. S. Keerthi等人在Improvements to Platt's SMO Algorithm for SVM Classifier Design一文中提出了对SMO算法的改进,纵观SMO ...
- POJ3280 - Cheapest Palindrome(区间DP)
题目大意 给定一个字符串,要求你通过插入和删除操作把它变为回文串,对于每个字符的插入和删除都有一个花费,问你把字符串变为回文串最少需要多少花费 题解 看懂题立马YY了个方程,敲完就交了,然后就A了,爽 ...
- 【原】用PHP搭建基于swoole扩展的socket服务(附PHP扩展的安装步骤及Linux/shell在线手册)
最近公司的一项目中,需要用PHP搭建一个socket服务. 本来PHP是不适合做服务的,因为和第三方合作,需要采用高效而稳定的TCP协议进行数据通信.经过多次尝试,最终选择了开源的PHP扩展:swoo ...
- JBOSS实现RMI时注意的问题
使用JBOSS部署EJB服务后通过RMI访问报错: javax.naming.CommunicationException: Could not obtain connection to any of ...
- spring + springmvc + jdbc + quartz + maven整合
个人搭建框架: pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="htt ...
- 我这个IOS渣渣又来写随笔了
我这个渣渣又来写了,哎,今天看了一篇大神面试别人的文章,感觉还有很遥远的路要走,不过没关系,还年轻,才入ios两个月,我相信自己的学习能力,加油! 博客园会是我成长的见证!
- Android源码是这样搞到的(图解)
Android学习到一定程度,就一定要多读代码多思考,Android源码就是很好的学习材料,本文就是把Android的源码下载下来.我们知道Android的源码是用Git这个分布式版本号控制工具管理的 ...
- RHCA学习笔记:RH442-Unit5 队列原理
NIT 5 Queuing Theory 队列原理 目标: 1.明白性能调优的关键术语 2. 应用队列技术解决性能问题 3.明白性能调优的复杂性 5.1 Introd ...