代码:

#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. hadoop入门必备基础知识

    1.对Linux 系统的要求        会基本的命令:        (1)知道root用户        (2)ls命令会查看文件夹内容        (3)cd命令等2.Java 的要求    ...

  2. hdu5593--ZYB's Tree(树形dp)

    问题描述 ZYB有一颗N个节点的树,现在他希望你对于每一个点,求出离每个点距离不超过KK的点的个数. 两个点(x,y)在树上的距离定义为两个点树上最短路径经过的边数, 为了节约读入和输出的时间,我们采 ...

  3. 几年的Git使用技巧总结

    用git有一年了,下面是我这一年来的git使用总结,覆盖了日常使用中绝大多数的场景.嗯,至少是够用一年了,整理出来分享给大家,不明白的地方可以回复交流. 创建和使用git ssh key 首先设置gi ...

  4. jQuery进行图片预先加载

    /** **将图片加载到内存中,在页面任何位置引用的时候,直接从缓存中读取,提升用户的体验,提高网站的流畅度.** **/ <!DOCTYPE html PUBLIC "-//W3C/ ...

  5. bootstrap折叠修改hover

    $('div.panel-collapse').on('shown.bs.collapse hidden.bs.collapse', function() { $(this).dequeue('col ...

  6. springboot 详细配置2

    # =================================================================== # COMMON SPRING BOOT PROPERTIE ...

  7. Linq to SQL 简单增删改查

    用Linq大大减少了对数据库的一般操作所需的编码量.运行下面事例之前,首先建一个叫做Alien的数据库表. CREATE TABLE [dbo].[Aliens](    [Id] [int] IDE ...

  8. 在CentOS6上安装Redis

    检查安装依赖程序 yum install -y gcc-c++ tcl wget 获取安装文件 wget http://download.redis.io/releases/redis-2.8.17. ...

  9. Android ListFragment实例Demo(自己定义适配器)

    上一篇文章介绍了ListFragment,当中的ListView并没有自己定义适配器,实际上在实际开发中常会用到自己定义适配器,是实现更复杂的列表数据展示. 所以这篇文章添加了自己定义适配器.来进行L ...

  10. int& a = b 的思考

    在PCL显示点云部分程序中,涉及到了如下程序 // 定义相关变量 pcl::PointCloud<pcl::PointXYZ>::Ptr cloud_ptr (new pcl::Point ...