// // main.m // FileManager // // Created by ganchaobo on 13-5-4. // Copyright (c) 2013年 ganchaobo. All rights reserved. // #import <Foundation/Foundation.h> void copyfile (){ //NSFileManager对文件进行(复制 创建,删除)等操作 NSFilehandler是对文件内容进行操作 NSFileManager *fileManager=[NSFileManager defaultManager]; // NSData *date=[NSData dataWithContentsOfFile:@"/Users/ganchaobo/Desktop/1.txt"]; //创建文件, BOOL isCreate=[fileManager createFileAtPath:@"/Users/ganchaobo/Desktop/a.txt" contents:nil attributes:nil]; if(isCreate){ NSLog(@"create success"); } else{ NSLog(@"create faied"); } NSFileHandle *inFileHandle=[NSFileHandle fileHandleForReadingAtPath:@"/Users/ganchaobo/Desktop/1.txt"]; //读取文件内容第一种de写法 // NSData *readData= [inFileHandle availableData]; //读取文件内容的第二种写法 NSData *readData= [inFileHandle readDataToEndOfFile];//read file data NSFileHandle *outFileHandle=[NSFileHandle fileHandleForWritingAtPath:@"/Users/ganchaobo/Desktop/a.txt"]; [outFileHandle writeData:readData]; [inFileHandle closeFile]; [outFileHandle closeFile]; } void Appendstringfile(){ NSFileHandle *filehandle=[NSFileHandle fileHandleForUpdatingAtPath:@"/Users/ganchaobo/Desktop/1.txt"]; //设置流的节点最后 //[filehandle seekToEndOfFile]; //设置流的节点到文件的一半位置 NSUInteger length= [[filehandle readDataToEndOfFile] length]/2; [filehandle seekToFileOffset:length]; //NSStringEncoding code= CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); NSString *str=@"我要追加数据"; NSData *date= [str dataUsingEncoding:NSUTF8StringEncoding]; [filehandle writeData:date]; [filehandle closeFile]; } int main(int argc, const char * argv[]) { @autoreleasepool { Appendstringfile(); } return 0; }