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 ...
随机推荐
- node系列1
NodeJS基础 JS是脚本语言,脚本语言都需要一个解析器才能运行,NodeJS就是一个解析器.nodejs.org 打开终端,键入node进入命令交互模式,可以输入一条代码语句后立即执行并显示结果 ...
- Linux 下svn恢复到某一版本
经常由于坑爹的需求,功能要切回到之前的某一个版本.有两种方法可以实现: 方法1: 用svn merge 1) 先 svn up,保证更新到最新的版本,如20: 2) 然后用 svn log ,查看历史 ...
- Jquery Ajax的时候 老是返回到 error,是因为json格式不正规的原因
Jquery Ajax的时候 老是返回到 error,是因为json格式不正规的原因: 怪不得不执行,原来我返回的是{success:true,id:1} 这种不规则的字符串,不是严格的json格式, ...
- 原来DataTable的Distinct竟如此简单![转]
本文转自:http://www.cnblogs.com/BlueFly/archive/2009/01/08/1372151.html 有时我们需要从DataTable中抽取Distinct数据,以前 ...
- hdoj 1898 Sempr == The Best Problem Solver?
Sempr == The Best Problem Solver? Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65535/3276 ...
- eas bos 编辑界面 editUIt 属性值为空
在编辑界面,我们可以正常的保存某个值到数据库,同时列表界面也可以正常显示. 但是当我们再次打开这个编辑界面的时候,该属性值缺无法显示. 通常情况下,通过下面两个方法可以解决 方法一: 在编辑界面重写一 ...
- nginx + lua 构建网站防护waf(一)
最近在帮朋友维护一个站点.这个站点是一个Php网站.坑爹的是用IIS做代理.出了无数问题之后忍无可忍,于是要我帮他切换到nginx上面,前期被不断的扫描和CC.最后找到了waf这样一个解决方案缓解一下 ...
- Sublime Text3使用及常用插件
1.安装packages组件: 参考一: https://sublime.wbond.net/installation 参考二: http://blog.csdn.net/superskk6/arti ...
- Notepad++中的颜色属性大全
Indent guideline style 缩进参考线的颜色 Brace highlight style 鼠标指针在框架左右时框架的颜色(如css中{} js中的()) Bad brace c ...
- AtomicLong
Spring package com.uniubi.management.controller; import java.util.concurrent.atomic.AtomicLong; impo ...