IOS Study Notes OC-API-file read and write, c-api-
//// WPFileHelper. m // OC-API-File Operations /// 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; // target to read the example file, such as suffix .txt if (tag = 0) {// method 1 use NSString NSError * error; NSString * filePath = @ "/Users/wangtouwang/Desktop/oc-controller .txt"; result = [NSString stringWithCo NtentsOfFile: 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) {// method 2 NSFileManager NSString * filePath = @ "/Users/wangtouwang/Desktop/oc-controller .txt"; NSFileManager * manager = [NSFileManager defaultManager]; NSData * data = [manager contentsAtPath: filePath]; resu Lt = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding]; // NSLog (@ "% @", result);} else if (tag = 2) {// method 3 NSFileHandle NSString * filePath = @ "/Users/wangtouwang/Desktop/oc-controller .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) {// Method 4 NSData NSString * filePath = @ "/Users/wangtouwang/Desktop/oc-controller .txt"; // NSDataReadingMappedIfSafe parameter. With this parameter, iOS will not map the entire file to the address space of the process instead of occupying the actual memory. This will solve the problem of full memory. 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; // The target reads the binary file, such as the image NSString * imagePath = @ "/Users/wangtouwang/Desktop/TEMP/51sPBOtpQ0L._SL500_AA300_.jpg"; if (tag = 0) {// method 1 NSData result = [NSData dataWithContentsOfFile: imagePath]; // NSInteger len = result. length; // NSLog (@ "length = % lu", len);} else if (tag = 1) {// method 2 NSFileHandle * handle = [NSFileHandle fileHandleForReadingAtPath: imagePath]; result = [handle readDataToEndOfFile]; [handle closeFile]; // method 3 NSFileManager} else if (tag = 2) {NSFileManager * manger = [NSFileManager defamanager]; result = [manger contentsAtPath: imagePath];} return result;} + (void) writerFileByString :( NSString *) str {NSString * content = str; NSString * filePath = @ "/Users/wangtouwang/Desktop/New file. txt"; // write the existing file example, such as suffix. txt. if the file does not exist, int tag = 3; if (tag = 0) {// method 1 NSString [content writeToFile: filePath atomically: YES encoding: NSUTF8StringEncoding error: nil];} else if (tag = 1) {// method 2 NSFileHandle NSData * data = [content dataUsingEncoding: handle]; NSFileHandle * handle = [NSFileHandle fileHandleForWritingAtPath: filePath]; [handle writeData: data]; [handle closeFile];} else if (tag = 2) {// method 3 NSFileManager if the file does not exist, NSFileManager * manager = [NSFileManager defamanager manager]; BOOL flag = [manager createFileAtPath: filePath contents: [content dataUsingEncoding: NSUTF8StringEncoding] attributes: nil]; if (flag) {NSLog (@ "Write Succeeded ");}} else if (tag = 3) {// Method 4 NSMutableData if the file does not exist still succeeded NSMutableData * writer = [[NSMutableData alloc] init]; [writer appendData: [content dataUsingEncoding: NSUTF8StringEncoding]; [writer writeToFile: filePath atomically: YES] ;}+ (void) writerFileByData :( NSData *) data {// write binary file sample file NSString * filePath = @ "/Users/wangtouwang/Desktop/new file .jpg"; int tag = 2; if (tag = 0) {// method 1 NSData [data writeToFile: filePath atomically: YES];} else if (tag = 1) {// method 2 NSFileHanle fails if the file does not exist, check whether the file exists first, and check whether you need to create NSFileHandle * handle = [NSFileHandle fileHandleForWritingAtPath: filePath]; [handle writeData: data]; [handle classCode];} else if (tag = 2) {// method 3 NSFileManager * manager = [NSFileManager defaultManager]; [manager createFileAtPath: filePath contents: data attributes: nil];} else if (tag = 3) {// Method 4 NSMutableData * writer = [[NSMutableData alloc] init]; [writer appendData: data]; [writer writeToFile: filePath atomically: YES] ;}}@ end