代码:

#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文件基本读写及操作的更多相关文章

  1. java对excel文件内容读写修改操作

    Read.java package domain; import java.io.FileInputStream; import java.io.InputStream; import jxl.Cel ...

  2. Linux下常用设置文件和文件夹读写权限操作

    1.查看权限 ls -l xxx.xxx (xxx.xxx是文件名)   2.常见权限 -rw------- (600) 只有所有者才有读和写的权限 -rw-r--r-- (644) 只有所有者才有读 ...

  3. Android中对文件的读写进行操作

    1. 在文件的地方生成一个read.txt文件,并且写入一个read数据.IO流用完之后一定要记得关闭. 对于try和catch是对于错误的抓取. 2. 首先先new file来找到那个文件,然后在通 ...

  4. OC文件操作(2)

    NSFileManager 文件管理器完成文件的创建.移动.拷贝等管理操作 1.查询文件和目录  OC中查询路径下的目录主要分为浅度遍历和深度遍历.  浅度遍历  NSFileManager * ma ...

  5. 『无为则无心』Python基础 — 41、Python中文件的读写操作(一)

    目录 1.文件操作步骤 2.文件的读写操作 (1)文件的打开 (2)打开文件模式 (3)获取一个文件对象 (4)关于文件路径 1.文件操作步骤 当我们要读取或者写入文件时,我们需要打开文件,在操作完毕 ...

  6. fopen()函数以"a+"方式打开一个不存在的文件后读写出现问题

    问题:在完成课后习题的时候,使用fopen()函数以"a+"方式打开一个不存在的文件时,写入.读取出现错误: //添加用户输入单词后,在单词头加入编号,确保编号跟着前面的开始排序 ...

  7. INI 文件的读写操作

    在C#中对INI文件进行读写操作,在此要引入using System.Runtime.InteropServices; 命名空间,具体方法如下: #region 变量 private static r ...

  8. Android 对 properties文件的读写操作

    -. 放在res中的properties文件的读取,例如对放在assets目录中的setting.properties的读取:PS:之所以这里只是有读取操作,而没有写的操作,是因为我发现不能对res下 ...

  9. C++学习48 对ASCII文件的读写操作

    如果文件的每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件(或称字符文件).程序可以从ASCII文件中读入若干个字符,也可以向它输出一些字符. 对ASCI ...

随机推荐

  1. Tsinsen A1516. fx 数位dp

    题目: http://www.tsinsen.com/A1516 A1516. fx 时间限制:2.0s   内存限制:256.0MB    总提交次数:164   AC次数:72   平均分:51. ...

  2. 【Kafka入门】Kafka入门第一篇:基础概念篇

    Kafka简介 Kafka是一个消息系统服务框架,它以提交日志的形式存储消息,并且消息的存储是分布式的,为了提供并行性和容错保障,消息的存储是分区冗余形式存在的. Kafka的架构 Kafka中包含以 ...

  3. 为静态Checkbox动态地添加checked属性

    1.ASP.NET HTML Code: 嵌套在repeater中 " ? "checked" : "" %> /> *** 关键代码: ...

  4. rx tx

  5. 【转】科普Spark,Spark是什么,如何使用Spark

    本博文是转自如下链接,为了方便自己查阅学习和他人交流.感谢原博主的提供! http://www.aboutyun.com/thread-6849-1-1.html http://www.aboutyu ...

  6. CSS的一些规范

    请使用简单的语法来链接样式表(type 属性不是必需的): <link rel="stylesheet" href="styles.css"> 短规 ...

  7. thinkphp3.2.3 版本使用redis缓存的时候无法使用认证

    我在使用thinkphp3.2.3的时候 发现如果是使用redis缓存 设置了认证的redis能连接成功 却无法 set 操作 ,检查发现是没有认证导致的  $redis->auth这一步没有, ...

  8. SGU107——987654321 problem

    For given number N you must output amount of N-digit numbers, such, that last digits of their square ...

  9. C++或者C#中如何拿到一个窗口的标题

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:C++或者C#中如何拿到一个窗口的标题.

  10. 关于lab4实验git+近期出国手续办理

    1.下载mit jos lab4时遇到问题(关于git操作,使用,还需进一步理解) 遇到的问题 出现未合并(merge)完全的问题,操作:git add kern/init.c 之后在确认提交 方法二 ...