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是否真的比普通 ...
随机推荐
- Oracle读书笔记
数据区(也叫数据扩展区)由一组连续的Oracle块所构成的Oracle存储结构,一个或多个数据块组成一个数据区,一个或多个数据区再组成一个断(Segment). 数据块是Oracle逻辑存储中的最小的 ...
- JavaScript:九种弹出对话框
[1.最基本的js弹出对话框窗口代码] 这是最基本的js弹出对话框,其实代码就几句非常简单: <script LANGUAGE="javascript"> <!- ...
- ubunu下用命令设置壁纸
ubunu下用命令设置壁纸: gsettings set org.gnome.desktop.background picture-uri “file:[fileName]” eg:gsettings ...
- Linux workqueue工作原理 【转】
转自:http://blog.chinaunix.net/uid-21977330-id-3754719.html 转自:http://bgutech.blog.163.com/blog/static ...
- spring.xml命名空间
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...
- 文本框textarea实时提示还可以输入多少文字
<!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content=&q ...
- MySQL与SQL比较有那些区别呢
MySQL是一个逐渐完善的过程,使用前期版本时会遇到一些问题,通常搞得莫名其妙,在版本选择上尽量选择最新的. 1.在5.03以前版本中,存储varchar型数据时,后面的空格会被忽视掉,前面的空格会保 ...
- Linux用户组与用户组基本命令
1.添加用户组:groupadd sexy2.修改组名:groupmod -n market sexy3.修改组编号:groupmod -g 668 market4.添加有编号的用户组:group - ...
- linux crontab定时执行
#利用crontab定时执行url研究了两种简单方式#一利用lynx访问url yum install lynxservice crond startcrontab -einsert键* * * * ...
- ACM题目————马拦过河卒
题目描述 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为“马拦过河卒”. ...