//
// 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-文件读写的更多相关文章

  1. Object-c学习之路六(oc字符串文件读写)

    // // main.m // NSString // // Created by WildCat on 13-7-25. // Copyright (c) 2013年 wildcat. All ri ...

  2. IOS学习笔记25—HTTP操作之ASIHTTPRequest

    IOS学习笔记25—HTTP操作之ASIHTTPRequest 分类: iOS2012-08-12 10:04 7734人阅读 评论(3) 收藏 举报 iosios5网络wrapper框架新浪微博 A ...

  3. iOS学习笔记-精华整理

    iOS学习笔记总结整理 一.内存管理情况 1- autorelease,当用户的代码在持续运行时,自动释放池是不会被销毁的,这段时间内用户可以安全地使用自动释放的对象.当用户的代码运行告一段 落,开始 ...

  4. iOS学习笔记总结整理

    来源:http://mobile.51cto.com/iphone-386851_all.htm 学习IOS开发这对于一个初学者来说,是一件非常挠头的事情.其实学习IOS开发无外乎平时的积累与总结.下 ...

  5. iOS学习笔记17-FMDB

    上一节我已经介绍了SQLite的简单使用,不了解的可以提前去看一下iOS学习笔记16-数据库SQLite,这节我们来讲下FMDB. 一.FMDB介绍 FMDB是一种第三方的开源库,FMDB就是对SQL ...

  6. iOS学习笔记17-FMDB你好!

    上一节我已经介绍了SQLite的简单使用,不了解的可以提前去看一下iOS学习笔记16-数据库SQLite,这节我们来讲下FMDB. 一.FMDB介绍 FMDB是一种第三方的开源库,FMDB就是对SQL ...

  7. Java学习笔记(04)

    Java学习笔记(04) 如有不对或不足的地方,请给出建议,谢谢! 一.对象 面向对象的核心:找合适的对象做合适的事情 面向对象的编程思想:尽可能的用计算机语言来描述现实生活中的事物 面向对象:侧重于 ...

  8. iOS学习笔记10-UIView动画

    上次学习了iOS学习笔记09-核心动画CoreAnimation,这次继续学习动画,上次使用的CoreAnimation很多人感觉使用起来很繁琐,有没有更加方便的动画效果实现呢?答案是有的,那就是UI ...

  9. iOS学习笔记之ARC内存管理

    iOS学习笔记之ARC内存管理 写在前面 ARC(Automatic Reference Counting),自动引用计数,是iOS中采用的一种内存管理方式. 指针变量与对象所有权 指针变量暗含了对其 ...

  10. iOS学习笔记之UITableViewController&UITableView

    iOS学习笔记之UITableViewController&UITableView 写在前面 上个月末到现在一直都在忙实验室的事情,与导师讨论之后,发现目前在实验室完成的工作还不足以写成毕业论 ...

随机推荐

  1. Codeforces149E - Martian Strings(KMP)

    题目大意 给定一个字符串T,接下来有n个字符串,对于每个字符串S,判断是否存在T[a-b]+T[c-d]=S(1 ≤ a ≤ b < c ≤ d ≤ length(T)) 题解 对于每个字符串S ...

  2. 8-13-Exercise

    链接:夜间活动 昨天的比赛好郁闷.......倒不是因为题目......在快要比赛的时候突然所有的网站都进不去了.......改了半天的DNS & IP......比赛都比了1个多小时才进去. ...

  3. PAT 1003. Emergency (25)

    1003. Emergency (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue As an emerg ...

  4. Android Fragment 生命周期

    启动的事件触发顺序 F Fragmeent A Activity onAttach(F) onAttachFragment(A) onCreate(F) onCreateView(F) onActiv ...

  5. 关于css中z-index 的应用

    我想很多人在应用中的会碰到这个问题,设置 z-index无效:无论设置为多高的数字都没有效果: 原因是在设置z-index之前必须满足一下两个条件: 1,给设置z-index的元素设置相应的定位值,p ...

  6. WinForm简单多国语言实现

    参考:http://minmin86121.blog.163.com/blog/static/4968115720119259151898/ http://www.cnblogs.com/hakuci ...

  7. JSON.NET概述

    1. JSON.NET概述 当JSON逐渐成为Ajax的标准数据交互格式时,在.NET中处理JSON数据只能使用字符串拼接的方法,十分麻烦,因而催生了JSON.NET这个项目. JSON.NET是一个 ...

  8. Android AsyncTask运作原理和源码分析

    自10年大量看源码后,很少看了,抽时间把最新的源码看看! public abstract class AsyncTask<Params, Progress, Result> {     p ...

  9. TransactionScope

    最近发现微软自带的TransactionScope(.Net Framework 2之后)是个好东东,提供的功能也很强大. 首先说说TransactionScope是什么,并能为我们做什么事情.其实看 ...

  10. linux ssh 中在window 传文件 下载文件 工具

    centos 安装工具 yum install lrzsz