Object-c:两种文件读写的对比
| 方式\操作 | 读 | 写 |
| 非URL方式 |
stringWithContentsOfFile |
writeToFile |
| URL方式 |
stringWithContentsOfURL |
writeToURL |
//
// main.m
// 字符串练习2:读写文件
//
// Created by Apple on 15/12/7.
// Copyright © 2015年 Apple. All rights reserved.
//
#import <Foundation/Foundation.h>
void readFile(NSString *path);
void writeToFile(NSString *path, NSString *str); int main(int argc, const char * argv[]) {
//读取文件中的内容
//NSString *path1 = @"/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/字符串练习2:读写文件/1.txt";
NSString *path1 = @"/Users/apple/Desktop/1.txt";
NSLog(@"读取文件:");
readFile(path1); //写入文件内容
NSString *path2 = @"/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/字符串练习2:读写文件/2.txt";
NSLog(@"写入文件");
NSString *str = @"这是一个测试";
writeToFile(path2,str); NSLog(@"读取文件:");
readFile(path2); return ;
} //读取文件
void readFile(NSString *path){
NSError *error = nil;
NSString *str = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];
if (error != nil) {
NSLog([error localizedDescription]);//将错误信息输出来
}
else{
NSLog(@"%@",str);
}
}
//写入文件
void writeToFile(NSString *path, NSString *str){
NSError *error = nil;
//atomically : YES时,没有写完,则会全部撤销;NO时候,没有写完,不会撤销
//注意:这种写入方式,如果文件补存在,则创建;如果文件存在,则覆盖原文件的内容
BOOL flag = [str writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:&error];//一般error都设置为nil,保证写入成功
if (flag) {
NSLog(@"写入成功");
}
else{
NSLog(@"写入失败");
}
}
NSString *path = @"file://192.168.1.103/Users/apple/Desktop/读写文件练习2/1.txt”;
//NSString *path = @"file:///Users/apple/Desktop/读写文件练习2/1.txt”;
//path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//比较老的方法,现在被下面的方法取代
path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; NSURL *url = [NSURL URLWithString:path]; NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; if (error == nil) {
NSLog(@"%@",str);
}
else{
NSLog(@"%@",[error localizedDescription]);
}
使用这个方法时,需要注意:
1)系统会帮我们自动加入file://,我们不需要再添加。再添加,路径就不对了。
2)即使URL中包含中文,都可以访问。系统会自动对包含的中文进行处理。所以一般开发中,访问本地资源,都使用这个方法。
NSString *path = @"/Users/apple/Desktop/读写文件练习2/1.txt”;
NSURL *url = [NSURL fileURLWithPath:path];
NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
if (error == nil) {
NSLog(@"%@",str);
}
else{
NSLog(@"%@",[error localizedDescription]);
}
//
// main.m
// 读写文件练习2
//
// Created by Apple on 15/12/7.
// Copyright © 2015年 Apple. All rights reserved.
// #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) {
//一.文件读取
//路径使用URL
//1.加载本地文件。注意:file://,不是file:///
NSString *path = @"file://192.168.1.103/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/读写文件练习2/1.txt"; //如果加载的是本地资源,那么URL上的主机地址可以不要
//注意:ip地址后面的斜杠不能省略!(其代表着跟路径)
//path = @"file:///Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/读写文件练习2/1.txt"; //如果路径中包含中文,先进行编码
//不编码的后果是:The file couldn’t be opened because the specified URL type isn’t supported.(URL类型不支持)
//path = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//比较老的方法,现在被下面的方法取代
//path = [path stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]]; //2.加载网络资源
//path = @"http://www.baidu.com"; //NSURL *url = [NSURL URLWithString:path]; //3.使用fileURLWithPath创建URL对象,加载本地资源。
/*
使用这个方法时,需要注意:
1)系统会帮我们自动加入file://,我们不需要再添加。再添加,路径就不对了。
2)即使URL中包含中文,都可以访问。系统会自动对包含的中文进行处理。所以一般开发中,访问本地资源,都使用这个方法。
*/
path = @"/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/读写文件练习2/1.txt";
NSURL *url = [NSURL fileURLWithPath:path]; NSError *error = nil; NSString *str = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];
if (error == nil) {
NSLog(@"%@",str);
}
else{
NSLog(@"%@",[error localizedDescription]);
} //二.文件写入
//路径使用URL
NSError *error2 = nil;
NSString *str2 = @"this is a test2"; /*
//第一种方式
NSString *path2 =@"file:///Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/读写文件练习2/2.txt";
path2 = [path2 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(@"path2 = %@",path2);
NSURL *url2 = [NSURL URLWithString:path2];
*/ //第二种方式
NSString *path2 = @"/Users/apple/Desktop/KeenApps/Object-C/Object-c-Test/读写文件练习2/2.txt";
NSURL *url2 = [NSURL fileURLWithPath:path2]; //注意:如果文件存在,则覆盖原文件的内容;如果文件不存在,则新建
[str2 writeToURL:url2 atomically:YES encoding:NSUTF8StringEncoding error:&error2];
if (error2 != nil) {
NSLog(@"%@", [error2 localizedDescription]);
}
else{
NSLog(@"文件写入成功!");
} return ;
}
Object-c:两种文件读写的对比的更多相关文章
- Java中的ReentrantLock和synchronized两种锁定机制的对比
问题:多个访问线程将需要写入到文件中的数据先保存到一个队列里面,然后由专门的 写出线程负责从队列中取出数据并写入到文件中. http://blog.csdn.net/top_code/article/ ...
- Shell 命令行求两个文件每行对比的相同内容
Shell 命令行求两个文件每行对比的相同内容 遇到的一个实际问题是,2017年08月01日起,所有未经实名的域名,全部停止解析.而我手上有不少域名,其中很多都是没有实名的.但我不知道哪些实名了,哪些 ...
- JSP下载txt 和 Excel两种文件
JSP下载txt 和 Excel两种文件 jsp 下载txt文件和excel文件 jsp 下载txt文件和excel文件 最近做了个用jsp下载的页面 将代码贴出来 权作记录吧 1 下载txt文件 ...
- Java多线程13:读写锁和两种同步方式的对比
读写锁ReentrantReadWriteLock概述 大型网站中很重要的一块内容就是数据的读写,ReentrantLock虽然具有完全互斥排他的效果(即同一时间只有一个线程正在执行lock后面的任务 ...
- OpenCV3.4两种立体匹配算法效果对比
以OpenCV自带的Aloe图像对为例: 1.BM算法(Block Matching) 参数设置如下: ) + ) & -; cv::Ptr<cv::StereoBM> b ...
- Spring Boot + Vue 前后端分离,两种文件上传方式总结
在Vue.js 中,如果网络请求使用 axios ,并且使用了 ElementUI 库,那么一般来说,文件上传有两种不同的实现方案: 通过 Ajax 实现文件上传 通过 ElementUI 里边的 U ...
- 基于python的selenium两种文件上传操作
方法一.input标签上传 如果是input标签,可以直接输入路径,那么可以直接调用send_keys输入路径,这里不做过多赘述,前文有相关操作方法. 方法二.非input标签上传 这种上传方 ...
- java多线程之:Java中的ReentrantLock和synchronized两种锁定机制的对比 (转载)
原文:http://www.ibm.com/developerworks/cn/java/j-jtp10264/index.html 多线程和并发性并不是什么新内容,但是 Java 语言设计中的创新之 ...
- NIO与普通IO文件读写性能对比
最近在熟悉java的nio功能.nio采用了缓冲区的方式进行文件的读写,这一点更接近于OS执行I/O的方式.写了个新旧I/O复制文件的代码,练练手,顺便验证一下两者读写性能的对比,nio是否真的比普通 ...
随机推荐
- 鸟哥的linux私房菜学习记录之查看帮助文档
如何使用man查看帮助文件 man command例如man date 按下enter键之后会显示date帮助文件,指令后面会有一个代号 info的用法 关机的几个常用命令 shutdown,halt ...
- Linux中文显示乱码解决
输入 echo $LANG可以查看当前使用的系统语言 查看是否有中文语言包可以在终端输入 locale命令,如有zh cn 表示已经安装了中文语言 没有则 yum groupinstall chine ...
- MongoDB C# / .NET Driver
MongoDB C# Driver是官方提供的.NET C#驱动. Getting Started with the C# Driver C# Driver Tutorial C# Driver LI ...
- Java获取字符串编码方式
直接下载吧: http://files.cnblogs.com/files/xiluhua/BytesEncodingDetectTool.rar
- Mac 终端命令汇总
OSX 的文件系统 OSX 采用的Unix文件系统,所有文件都挂在跟目录 / 下面,所以不在要有Windows 下的盘符概念. 你在桌面上看到的硬盘都挂在 /Volumes 下. 比如接上个叫做 US ...
- symfony中twig的模板过滤器
过滤器 变量可以被过滤器修饰.过滤器和变量用(|)分割开.过滤器也是可以有参数的.过滤器也可以被多重使用. 通用过滤器 date过滤器 1.1版本新增时区支持,1.5版本增加了默认的日期格式.格式化时 ...
- java IO和NIO 的区别
Java NIO和IO的主要区别 下表总结了Java NIO和IO之间的主要差别. IO NIO 面向流 面向缓冲 阻塞IO 非 ...
- 用canvas画时钟
效果图在博客首页上. html: <canvas id="canvas" >Your browser does not support canvas</canva ...
- ACM题目————士兵杀敌(四)
描述 南将军麾下有百万精兵,现已知共有M个士兵,编号为1~M,每次有任务的时候,总会有一批编号连在一起人请战(编号相近的 人经常在一块,相互之间比较熟悉),最终他们获得的军功,也将会平分到每个人身上, ...
- SDUT 2411:Pixel density
Pixel density Time Limit: 1000MS Memory limit: 65536K 题目描述 Pixels per inch (PPI) or pixel density is ...