文件管理习题练习二
2014-03-19 08:43
399 查看
//
// TImer.m
// GetTime
//
// Created by Gaowz on 14-3-18.
// Copyright (c) 2014年 Gaowz 无限互联3G学院. All rights reserved.
//
#import "TImer.h"
@implementation TImer
- (void)startTimer {
[NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(timerAction:)
userInfo:nil
repeats:YES];
}
- (void)timerAction:(NSTimer *)timer {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *path =NSHomeDirectory();
NSString *filePath = [path stringByAppendingPathComponent:@"Date.text"];
//获取当前时间
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yyyy/MM/dd HH:mm:ss \n"];
NSString *formatterDate = [formatter stringFromDate:[NSDate date]];
NSLog(@"%@",formatterDate);
NSData *data = [formatterDate dataUsingEncoding:NSUTF8StringEncoding];
if (index == 1) {
//创建文件
[fileManager createFileAtPath:filePath
contents:data
attributes:nil];
}else {
//创建NSFileHandle对象
NSFileHandle *fileHandle = [NSFileHandle fileHandleForUpdatingAtPath:filePath];
[fileHandle seekToEndOfFile];
//写入文件
[fileHandle writeData:data];
[fileHandle closeFile];
}
if (index == 8) {
[timer invalidate];
}
index++;
}
@end
//
// main.m
// GetTime
//
// Created by Gaowz on 14-3-18.
// Copyright (c) 2014年 Gaowz 无限互联3G学院. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "TImer.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
TImer *p = [[TImer alloc]init];
[p startTimer];
[[NSRunLoop currentRunLoop] run];
}
return 0;
}
相关文章推荐
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- Linux基础之命令练习Day3-文件管理:cat,tar,gzip,vim,ln
- 关于文件管理输入输出的练习
- Linux 文件与目录管理+用户管理命令(详解+练习)
- Chapter 7 Linux文件与目录管理——习题
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 《鸟哥的Linux私房菜》7章 Linux文件与目录管理 习题答案
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- Linux基础之命令练习Day3-文件管理:cat,tar,gzip,vim,ln
- Linux学习笔记02 文件权限管理练习
- 第三周作业——工资管理系统(练习文件操作)
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C6_函数多文件管理练习
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 宿舍管理系统【C语言建工程及多文件练习】
- linux初学<习题二>管理刚才新建立的文件!
- C++文本查询程序 不要定义类和智能指针管理数据 C++Primer练习12.28 使用vector,map,set容器保存来自文件的数据并生成查询结果
- 练习Perl单文件实现图书管理功能