OC-文件和计时器操作实例
来源:互联网 发布:新手学办公软件 编辑:程序博客网 时间:2024/06/05 23:56
Example:创建一个Date.text文件,获取当前的日期,将日期存储为格式”2014/08/22 10:25:23“的形式。然后一秒钟记录一次,将新的时间存入到文件中。
WriteDate.m的内容:
- (void)runWrite {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path = NSHomeDirectory();
NSString *filePath = [path stringByAppendingPathComponent:@"Date.text"];
BOOL success = [fileManager createFileAtPath:filePath contents:nil attributes:nil];
if (success) {
NSLog(@"created success");
}
NSFileHandle *fileHandle = [NSFileHandle fileHandleForWritingAtPath:filePath];
[NSTimer scheduledTimerWithTimeInterval:1 target: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++;
}
main函数的内容:
int main(int argc,const char * argv[ ])
{
@autoreleasepool {
WriteDate *writeDate = [[WriteDate alloc]init];
[writeDate runWrite];
}
[[NSRunLoop currentRunLoop] run];
}
- OC-文件和计时器操作实例
- OC 文件操作 创建和删除API
- OC 文件路径获取和操作
- oc中的文件和目录相关操作
- OC-文件读取实例
- Android开发Timer计时器的操作实例
- 【OC学习-24】实例:创建一个文件并连续写入10次当前时间——巩固文件操作和熟悉定时器操作
- OC文件操作
- OC文件操作
- OC的文件操作
- OC中的文件操作
- OC-文件操作
- oc- 文件操作
- OC学习--文件操作
- oc文件操作
- oc 之文件操作
- 【OC】文件操作相关
- OC文件操作
- 2014年八月的面试题备忘
- ruby gem相关命令使用
- Spring开发入门
- Javascript回调函数和函数重载的用法
- Tomcat问题总结
- OC-文件和计时器操作实例
- 双微营销的坎坷路
- MongoDB基本操作
- linux基础指令
- ==和equals问题
- 织梦dedecms生成select下拉式友情链接列表的方法
- 有佰佰安品,就没有绝望的人生!
- Android wakelock 学习总结第一篇
- js 判断qq是否在线方法