IOS 学习笔记 2015-04-03 OC-API-文件读写
//
// WPFileHelper.m
// OC-API-文件操作
//
// Created by wangtouwang on 15/4/3.
// Copyright (c) 2015年 wangtouwang. All rights reserved.
// #import "WPFileHelper.h" @implementation WPFileHelper +(NSString *)getFileToString:(int)tag{
NSString *result ;
//目标 读取字符型文件 例如后缀.txt
if (tag==) {
//方法1 使用 NSString
NSError *error;
NSString *filePath =@"/Users/wangtouwang/Desktop/OC-控件.txt";
result = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:&error];
if (error) {
NSLog(@"read error ,the error is %@",error);
}else{
NSLog(@"read success,the file content is %@",result);
}
}else if (tag==){
//方法2 使用 NSFileManager
NSString *filePath =@"/Users/wangtouwang/Desktop/OC-控件.txt";
NSFileManager *manager=[NSFileManager defaultManager];
NSData *data = [manager contentsAtPath:filePath];
result= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//NSLog(@"%@",result);
}else if (tag==){
//方法3 NSFileHandle
NSString *filePath =@"/Users/wangtouwang/Desktop/OC-控件.txt";
NSFileHandle *handler = [NSFileHandle fileHandleForReadingAtPath:filePath];
NSData *data = [handler readDataToEndOfFile];
result= [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
[handler closeFile];
//NSLog(@"%@",result);
}else if(tag==){
//方法4 NSData
NSString *filePath =@"/Users/wangtouwang/Desktop/OC-控件.txt";
//NSDataReadingMappedIfSafe参数。使用这个参数后,iOS就不会把整个文件全部读取的内存了,而是将文件映射到进程的地址空间中,
//这么做并不会占用实际内存。这样就可以解决内存满的问题。
NSData *data= [NSData dataWithContentsOfFile:filePath options:nil error:nil];
result =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"%@",result);
}
return result;
} +(NSData *) getFileToData:(int)tag{
NSData *result = nil;
//目标 读取二进制文件 例如 图片
NSString *imagePath = @"/Users/wangtouwang/Desktop/TEMP/51sPBOtpQ0L._SL500_AA300_.jpg";
if (tag==) {
// 方法1 NSData
result = [NSData dataWithContentsOfFile:imagePath];
// NSInteger len = result.length;
//NSLog(@"长度 = %lu",len);
}else if (tag == ){
//方法2 NSFileHandle
NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:imagePath];
result =[handle readDataToEndOfFile];
[handle closeFile];
//方法3 NSFileManager
}else if(tag==){
NSFileManager *manger = [NSFileManager defaultManager];
result = [manger contentsAtPath:imagePath];
}
return result;
} +(void)writerFileByString:(NSString *)str{
NSString *content = str;
NSString *filePath =@"/Users/wangtouwang/Desktop/新文件.txt";
// 写入字符型文件 例如后缀.txt 假如文件不存在依然成功
int tag =;
if (tag==) {
//方法1 NSString
[content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
}else if (tag==){
//方法2 NSFileHandle 假如文件不存在会失败
NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
[handle writeData:data];
[handle closeFile];
}else if(tag==){
//方法3 NSFileManager 假如文件不存在依然成功
NSFileManager *manager = [NSFileManager defaultManager];
BOOL flag = [manager createFileAtPath:filePath contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];
if (flag) {
NSLog(@"写入成功");
}
}else if (tag==){
//方法4 NSMutableData 假如文件不存在依然成功
NSMutableData *writer = [[NSMutableData alloc] init];
[writer appendData:[content dataUsingEncoding:NSUTF8StringEncoding]];
[writer writeToFile:filePath atomically:YES];
}
} +(void)writerFileByData:(NSData *)data{
//写入二进制文件 例如图片
NSString *filePath =@"/Users/wangtouwang/Desktop/新文件.jpg";
int tag =;
if (tag==) {
//方法1 NSData
[data writeToFile:filePath atomically:YES];
}else if(tag==){
//方法2 NSFileHanle 假如文件不存在则会失败,应该先判断是否存在,再看是否需要创建
NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];
[handle writeData:data];
[handle classCode];
}else if(tag==){
//方法3 NSFileManager
NSFileManager *manager= [NSFileManager defaultManager];
[manager createFileAtPath:filePath contents:data attributes:nil];
}else if (tag==){
// 方法4 NSMutableData
NSMutableData *writer = [[NSMutableData alloc] init];
[writer appendData:data];
[writer writeToFile:filePath atomically:YES];
}
} @end
IOS 学习笔记 2015-04-03 OC-API-文件读写的更多相关文章
- Object-c学习之路六(oc字符串文件读写)
// // main.m // NSString // // Created by WildCat on 13-7-25. // Copyright (c) 2013年 wildcat. All ri ...
- IOS学习笔记25—HTTP操作之ASIHTTPRequest
IOS学习笔记25—HTTP操作之ASIHTTPRequest 分类: iOS2012-08-12 10:04 7734人阅读 评论(3) 收藏 举报 iosios5网络wrapper框架新浪微博 A ...
- iOS学习笔记-精华整理
iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...
- iOS学习笔记总结整理
来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...
- iOS学习笔记17-FMDB
上一节我已经介绍了SQLite的简单使用,不了解的可以提前去看一下iOS学习笔记16-数据库SQLite,这节我们来讲下FMDB. 一.FMDB介绍 FMDB是一种第三方的开源库,FMDB就是对SQL ...
- iOS学习笔记17-FMDB你好!
上一节我已经介绍了SQLite的简单使用,不了解的可以提前去看一下iOS学习笔记16-数据库SQLite,这节我们来讲下FMDB. 一.FMDB介绍 FMDB是一种第三方的开源库,FMDB就是对SQL ...
- Java学习笔记(04)
Java学习笔记(04) 如有不对或不足的地方,请给出建议,谢谢! 一.对象 面向对象的核心:找合适的对象做合适的事情 面向对象的编程思想:尽可能的用计算机语言来描述现实生活中的事物 面向对象:侧重于 ...
- iOS学习笔记10-UIView动画
上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...
- iOS学习笔记之ARC内存管理
iOS学习笔记之ARC内存管理 写在前面 ARC(Automatic Reference Counting),自动引用计数,是iOS中采用的一种内存管理方式. 指针变量与对象所有权 指针变量暗含了对其 ...
- iOS学习笔记之UITableViewController&UITableView
iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...
随机推荐
- 注册表-恶意首页追踪之旅(IE不能改主页)
恶意首页追踪之旅(先说下,360无法修复这个恶意首页) 话说,今天下了个扫站的工具,结果一不小心中了恶意广告! 中招后不停的乱下东西安装,360不停的在那弹出提示! 无语了,一个个卸载,把C:\win ...
- 关闭utorrent的广告
版本:3.4.9 / 方法来源:wikihow. 在"选项-高级"里将下面的选项全部改成false. offers.left_rail_offer_enabledoffers.sp ...
- python学习之列表
#coding:utf-81.#reverse方法将列表的元素反向存放,改变了原列表但不返回值x=[5,2,4,3,8]x.reverse() #x[::-1] 不改变list反向排序print x ...
- solrj6.2异常--Expected mime type application/octet-stream but got text/html.
org.apache.solr.client.solrj.impl.HttpSolrClient$RemoteSolrException: Error from server at http://19 ...
- Spring Batch Framework– introduction chapter(下)
Extract,Transform, and load(ETL) Briefly stated, ETL is a process in the database anddata-warehousin ...
- sql转Linq的工具
本文转载:http://www.cnblogs.com/huangxincheng/archive/2011/05/12/2044990.html 介绍一个小工具 Linqer 这些天写Linq挺 ...
- 每天进步一点达——MySQL——myisampack
一. 简单介绍 myisampack是一个压缩使用MyISAM引擎表的工具,通常会压缩40%~70%,当须要訪问数据.server会将所须要的信息读入到内存中.所以当訪问详细记录时,性能 ...
- URAL - 1736 - Chinese Hockey
题意:n支队伍打比赛,每2队只进行1场比赛,规定时间内胜得3分,败得0分,若是打到了加时赛,那么胜得2分,败得1分,给出n支队伍最后的总得分,问这个结果是否是可能的,是的话输出“CORRECT”及各场 ...
- Java实现希尔排序(增量递减排序)
package Insert.sort; import java.util.Scanner; /*又叫缩小增量排序,本质是插入排序,将待排的序列增量分成几个子序列,分别对每个子序列进行直接插入排序 * ...
- oracle修改字段类型
有一个表名为tb,字段段名为name,数据类型nchar(20). 1.假设字段数据为空,则不管改为什么字段类型,可以直接执行:alter table tb modify (name nvarchar ...