创建一个Date.text文件,获取当前的日期,将日期格式为“2013/02/14 05:20:00”的形式。然后一秒钟记录一次,将新的时间存入到文件中。
来源:互联网 发布:h3c stp 阻塞端口 编辑:程序博客网 时间:2024/06/06 01:51
打开xcode
点击 create a new xcode project
点击 左侧 Application 选择右侧中的 Command Line Tool(相当于windows下的command)
--- next
product Name:NSFileHandleTask1
Company identifier:com.imti
type: foundation 是一个framework
取消选择 use Automatic Reference Counting
--- next
--- create
new file...
name:WriteDate
subclass: NSObject
WriteDate.h
加入
- (void)runWrite;
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(@"create 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];
[dateformate release];
datestring = [datestring stringByAppendingString:@"\n"];
NSData *data = [datestring dataUsingEncoding:NSUTF8StringEncoding];
[fileHandle writeData:data];
if (n == 10) {
[timer invalidate];//停止定时器
//关闭文件
[fileHandle closeFile];//关闭管道
}
n++;
}
时间定时器 NSTimer 一个能够定时的完成任务的类
说明:
scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
预订一个Timer,设置一个时间间隔。
表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1
target:(id)aTarget
表示发送的对象,如self
selector:(SEL)aSelector
方法选择器,在时间间隔内,选择调用一个实例方法
userInfo:(id)userInfo
此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器。
repeats:(BOOL)yesOrNo
当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。
timer开始
[tm fier];
timer停止:
[tm invalidate];
timer是否已经开始在进行中:
BOOL b = [tm isValid];
点击 create a new xcode project
点击 左侧 Application 选择右侧中的 Command Line Tool(相当于windows下的command)
--- next
product Name:NSFileHandleTask1
Company identifier:com.imti
type: foundation 是一个framework
取消选择 use Automatic Reference Counting
--- next
--- create
new file...
name:WriteDate
subclass: NSObject
WriteDate.h
加入
- (void)runWrite;
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(@"create 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];
[dateformate release];
datestring = [datestring stringByAppendingString:@"\n"];
NSData *data = [datestring dataUsingEncoding:NSUTF8StringEncoding];
[fileHandle writeData:data];
if (n == 10) {
[timer invalidate];//停止定时器
//关闭文件
[fileHandle closeFile];//关闭管道
}
n++;
}
时间定时器 NSTimer 一个能够定时的完成任务的类
说明:
scheduledTimerWithTimeInterval:(NSTimeInterval)seconds
预订一个Timer,设置一个时间间隔。
表示输入一个时间间隔对象,以秒为单位,一个>0的浮点类型的值,如果该值<0,系统会默认为0.1
target:(id)aTarget
表示发送的对象,如self
selector:(SEL)aSelector
方法选择器,在时间间隔内,选择调用一个实例方法
userInfo:(id)userInfo
此参数可以为nil,当定时器失效时,由你指定的对象保留和释放该定时器。
repeats:(BOOL)yesOrNo
当YES时,定时器会不断循环直至失效或被释放,当NO时,定时器会循环发送一次就失效。
timer开始
[tm fier];
timer停止:
[tm invalidate];
timer是否已经开始在进行中:
BOOL b = [tm isValid];
0 0
- 创建一个Date.text文件,获取当前的日期,将日期格式为“2013/02/14 05:20:00”的形式。然后一秒钟记录一次,将新的时间存入到文件中。
- 将一个字符串转换为datatime时,先分析该字符串以获取的日期,然后再将每个变量放置到datetime对象中
- java 如何将日期类型Date 时间戳 转换为 期望格式的 日期字符串(mongo db)
- 将一个图片以二进制值的形式存入Xml文件中
- 将一个图片以二进制值的形式存入Xml文件中
- 将一个图片以二进制值的形式存入Xml文件中
- 将new Date()获取的日期格式化
- shell的date日期循环方法:日期格式转时间戳计算,再将时间戳转回日期格式
- java中如何将String类型的日期格式为yyyyMMdd转化为date类型的yyyy-MM-dd格式
- log4j自定义级别并将新级别日志信息输出到指定带日期格式的log文件
- log4j自定义级别并将新级别日志信息输出到指定带日期格式的log文件
- Java 以指定日期时间格式获取当前时间、以及每隔一秒刷新一次的方法------附带实例
- 将输入的字符存入到指定的文件中
- java将日期时间字符串转成日期插入到oracle的date字段
- 将String格式转换为Date格式,并计算两个日期的差值
- 怎么将一个整个文件一次读到内存,然后循环从内存中读取1024字节,写入新的文件(或socket发送出去)
- 关于new Date()获取到Date时间后的日期格式修改
- 将当前时间戳转化为 struct tm 结构 然后获取自己想要的数据
- js内置对象(string字符串对象)
- 九度OJ 题目1081:递推数列
- 读取文件类和常用方法
- 友谊赛 最小生成树
- 自动启动调试器
- 创建一个Date.text文件,获取当前的日期,将日期格式为“2013/02/14 05:20:00”的形式。然后一秒钟记录一次,将新的时间存入到文件中。
- BZOJ 2725 Violet 6 故乡的梦 堆优化Dijkstra+线段树
- HDU 5015 矩阵快速幂
- Linux根文件系统构建
- C语言字符串总结
- 常用的基本控件TextView、富文本和RadioButton
- FOUNDATION
- IOS——TableView 中利用Item模型进行 Cell 的开发(2)Item 模型篇
- 三星应用商店注册的二进制 不符合 GALAXY Specials 的类别条件解决办法