OC基础:7.1 7.2 7.3 NSFileHandle 和 NSFileManager学习

来源:互联网 发布:网络教育双重学籍 编辑:程序博客网 时间:2024/05/16 01:09



复制一个文件:

 @autoreleasepool {        NSString *homePah = NSHomeDirectory();        NSLog(@"%@",homePah);        NSString *srcPath = [homePah stringByAppendingPathComponent:@"iOS.pdf"];        NSString *tagetPath = [homePah stringByAppendingPathComponent:@"iOS_bak.pdf"];                NSFileManager *fileManager = [NSFileManager defaultManager];        BOOL success = [fileManager createFileAtPath:tagetPath contents:nil attributes:nil];        if (success) {            NSLog(@"创建成功");        }                NSFileHandle *inFile = [NSFileHandle fileHandleForReadingAtPath:srcPath];        NSFileHandle *outFile = [NSFileHandle fileHandleForWritingAtPath:tagetPath];                NSDictionary *fileAttr = [fileManager attributesOfItemAtPath:srcPath error:nil];        NSNumber *fileSizeNum = [fileAttr objectForKey:NSFileSize];                BOOL isEnd = YES;        NSInteger readSize = 0;        NSInteger fileSize = [fileSizeNum longValue];                while (isEnd) {            NSInteger subLeng = fileSize - readSize;            NSData *data = nil;            if (subLeng < 500) {                isEnd = NO;                data = [inFile readDataToEndOfFile];            }else{                data = [inFile readDataOfLength:500];                readSize += 500;                [inFile seekToFileOffset:readSize];            }                        [outFile writeData:data];        }                [outFile closeFile];    }


追加数据:

/*追加数据*/NSString *homePath = NSHomeDirectory();NSString *sourcePaht = [homePath stringByAppendingPathComponent:@"testfile.txt"];NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:sourcePaht];//将节点调到文件末尾[fileHandle seekToEndOfFile];//跳转到指定的位置//[fileHandle seekToFileOffSet:10]; //在10的位置开始插入数据,会覆盖原来里面的数据。NSString *str = @"追加的数据";NSData *stringData = [str dataUsingEncoding:NSUTF8StringEncoding];//追加写入数据[fileHandle writeData:stringData];[fileHandle closeFile];/*定位读取数据*/NSFileManager *fm = [NSFileManager defaultManager];NSString *content = @"dfsdfdasfsadfsadf";[fm createFileAtPath:path contents:[content dataUsingEncoding:NSUTF8StringEncoding] attributes:nil];NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:path];//读取数据长度NSUInteger length = [[fileHandle availableData] length];//偏移量设置为文件一半[fileHandle seekToFileOffSet:length/2];//从一半读到文件最后NSData *data = [fileHandle readDataToEndOfFile];[fileHandle closeFile];NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];NSLog(@"%@", str);/**复制文件,*/NSString *homePath = NSHomeDirectory();NSString *sourcePaht = [homePath stringByAppendingPathComponent:@"testfile.txt"];NSString *tagerPath = [homePath stringByAppendingPathComponent: @"collphone.bak.txt"];NSFileManager *fileManager = [NSFileManager defaultManager];//创建目标文件BOOL success = [fileManager createFileAtPath:tagerPath contents:nil attributes:nil];if(success){NSLog(@"create success");}//读取原文件NSFileHandle *outFileHandle = [NSFileHandle fileHandleForWritingAtPath:tagerPath];NSFileHandle *inFileHandle = [NSFileHandle fileHandleForReadingAtPath:sourcePaht ];//[inFileHandle availableData] // 读取所有数据NSData *date = [inFileHandle readDataToEndOfFile];[outFileHandle writeData:data];[outFileHandle closeFile];[inFileHandle closeFile];

定时器操作:

-(void)runWrite{NSFileManager *fileManager = [NSFileManager defaultManager];NSString *path = NSHomeDirectory();NSString *filepath = [path stringByAppendingPathComponent:@"Date.txt"];BOOL success = [fileManager createFileAtPath:filepath contents:nil attributes:nil];if(success){NSLog("create success");}NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filepath];[NSTimer scheduledTimerWithTimeInterval:1 targer:self selector:@selector(timerAction:)  userInfo:fileHandle repeats:YES];  }-(void)timerAction:(NSTimer *)timer{    static int n = 0;NSFileHandle *fileHandle = timer.userInfo;[fileHandle seekToEndOfFile]; //定位到文件末尾NSDate *nowDate = [NSDate date];NSDateFormatter *dateformate = [[NSDateFormatter alloc]init];[dateformate setDateFormat:@"yyyy/MM/dd HH:mm:ss"];NSString *datestring = [dateformate stringFromDate:nowDate];datestring = [datestring stringByAppendingString:@"\n"];NSData *data = [datestring dataUsingEncoding:NSUTF8StringEncoding];[fileHandle writeData:data];if(n == 10){[timer invalidate]; //定时器停止[fileHandle closeFile]; //关闭管道}n++;}

 
1 0