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是否真的比普通 ...
随机推荐
- 查看innodb表空间
使用脚本innodb_space,关于innodb的页管理方式可以参考Jeremy Cole的innodb的页管理方式, innodb_space -f test/t.ibd space-page-t ...
- ubunu下用命令设置壁纸
ubunu下用命令设置壁纸: gsettings set org.gnome.desktop.background picture-uri “file:[fileName]” eg:gsettings ...
- Spring JDBC保存枚举对象含关键字报错原因之一
报错信息: org.springframework.jdbc.UncategorizedSQLException: PreparedStatementCallback; uncategorized S ...
- Docker Centos安装Redis以及问题处理
之前一篇文章 Redis安装及主从配置 介绍了redis的安装配置,另一篇文件介绍了 Docker Centos安装Openssh .今天将两篇文件结合一下——在Docker Centos环境下搭建r ...
- ug-Assertion failure in [MyClass layoutSublayersOfLayer:]
这是在iOS7上,tableview 的sectionHeaderView中报错 *** Assertion failure in -[****.****UITVSectionHeader_Team ...
- Codeforces 735D:Taxes(哥德巴赫猜想)
http://codeforces.com/problemset/problem/735/D 题意:给出一个n,这个n可以分解成 n = n1 + n2 + -- + nk,其中k可以取任意数.要使得 ...
- greenplum如何激活,同步,删除standby和恢复原始master
在Master失效时,同步程序会停止,Standby可以被在本机被激活,激活Standby时,同步日志被用来恢复Master最后一次事务成功提交时的状态.在激活Standby时还可以指定一个新的Sta ...
- bianwu 哈希表输出到 excel
一.输出到excel 函数: protected void InputFileTheme(object[] Header,object [] DataFileds,string sql,string ...
- 过滤器(Filter)案例:检测用户是否登陆的过滤器
*****检测用户是否登陆的过滤器:不需要用户跳转到每个页面都需要登陆,访问一群页面时,只在某个页面上登陆一次,就可以访问其他页面: 1.自定义抽象的 HttpFilter类, 实现自 Filter ...
- JAVA基础知识之多线程——三种实现多线程的方法及区别
所有JAVA线程都必须是Thread或其子类的实例. 继承Thread类创建线程 步骤如下, 定义Thead子类并实现run()方法,run()是线程执行体 创建此子类实例对象,即创建了线程对象 调用 ...