OC-文件和计时器操作实例
2014-08-22 10:54
316 查看
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];
}
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学习-24】实例:创建一个文件并连续写入10次当前时间——巩固文件操作和熟悉定时器操作
- Java文件操作实例
- C#操作VFP的dbf数据库文件实例
- .NET中的文件IO操作实例
- C#中FileInfo类操作文件实例
- ini文件操作实例
- 实例介绍文件下载,注册表操作等基本技巧!(原创)
- 低级文件操作实例代码
- C++简单文件操作实例
- php文件操作实例——小型留言本
- C#文件读写IO操作 实例
- 低级文件操作实例代码
- C#操作文件之 按行写入文件实例
- 文件上传[多文件上传与简单操作文件上传实例(文件上传,自动重命名,自动添加文件夹)]【深海原创】
- 目录文件操作实例[3]_使用文件预览显示文件信息
- 利用application,cookies,sessino以及文件文件操作制作计数器和投票的综合实例(按学习进程更新)
- 目录文件操作实例[1]_列出本地硬盘上的所有驱动器
- 文件上传[多文件上传与简单操作文件上传实例(文件上传,自动重命名,自动添加文件夹)]
- .NET中的文件IO操作实例
- ASP.NET 文件操作实例