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==0) {

        //方法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==1){

        //方法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==2){

        //方法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==3){

        //方法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==0) {

        // 方法1 NSData

        result = [NSData dataWithContentsOfFile:imagePath];

       // NSInteger len = result.length;

        //NSLog(@"长度 = %lu",len);

    }else if (tag == 1){

        //方法2 NSFileHandle

        NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:imagePath];

       result =[handle readDataToEndOfFile];

        [handle closeFile];

        //方法3 NSFileManager

    }else if(tag==2){

        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 =3;

    if (tag==0) {

        //方法1 NSString

        [content writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];

    }else if (tag==1){

        //方法2  NSFileHandle 假如文件不存在会失败

        NSData *data = [content dataUsingEncoding:NSUTF8StringEncoding];

        NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];

        [handle writeData:data];

        [handle closeFile];

    }else if(tag==2){

        //方法3 NSFileManager 假如文件不存在依然成功

        NSFileManager *manager = [NSFileManager defaultManager];

        BOOL flag = [manager createFileAtPath:filePath contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];

        if (flag) {

            NSLog(@"写入成功");

        }

    }else if (tag==3){

        //方法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 =2;

    if (tag==0) {

        //方法1 NSData

        [data writeToFile:filePath atomically:YES];

    }else if(tag==1){

        //方法2 NSFileHanle 假如文件不存在则会失败,应该先判断是否存在,再看是否需要创建

        NSFileHandle *handle = [NSFileHandle fileHandleForWritingAtPath:filePath];

        [handle writeData:data];

        [handle classCode];

    }else if(tag==2){

        //方法3 NSFileManager

        NSFileManager *manager= [NSFileManager defaultManager];

        [manager createFileAtPath:filePath contents:data attributes:nil];

    }else if (tag==3){

        // 方法4 NSMutableData

        NSMutableData *writer = [[NSMutableData alloc] init];

        [writer appendData:data];

        [writer writeToFile:filePath atomically:YES];

    }

}





@end

 

 

你可能感兴趣的:(学习笔记)