IOS文件常用操作 NSFileManager----不断更新中
2013-06-02 17:21
323 查看
============================================================博文原创,转载请声明出处电子咖啡-专注于移动互联网
============================================================
-------文件的创建,遍历,得到文件属性(创建日期等),删除等操作
直接从工程里面扒出来的,不过写的很清晰。
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
[dateFormatter setDateFormat:@"yyyy_MM_dd"];
NSString* dataStr = [dateFormatter stringFromDate:[NSDate date]];
NSString *dicpath = [NSString stringWithFormat:@"%@/Documents/logs",NSHomeDirectory()];
NSString *path = [NSString stringWithFormat:@"%@/log%@.html",dicpath,dataStr];
NSFileManager *manager = [NSFileManager defaultManager];
//如果不存在当天的日志,则新建
if ([manager fileExistsAtPath:path] == NO) {
//删除max interval 以上的log
NSFileManager* fm= [[[NSFileManager alloc] init]autorelease];
NSArray *levelList = [fm contentsOfDirectoryAtPath:dicpath error:nil ] ;
for (NSString *fname in levelList) {
NSString *fpath = [NSString stringWithFormat:@"%@/%@",dicpath,fname];
NSDictionary *fileAttributes = [manager attributesOfItemAtPath:fpath error:nil];
NSDate * creationDate=nil;
if ((creationDate = [fileAttributes objectForKey:NSFileCreationDate])) {
NSTimeInterval interval = [creationDate timeIntervalSinceNow];
//printf("%s's interval is: %f\n",[fpath UTF8String ],interval);
if ((interval*-1) >MAX_INTERVAL*24*60*60) {
[manager removeItemAtPath:fpath error:nil];
//NSLog(@"delete file:%@ \n",fpath );
}
}
}
//创建新log
[manager createDirectoryAtPath:dicpath withIntermediateDirectories:YES attributes:nil error:nil];
[manager createFileAtPath:path contents:nil attributes:nil];
///Users/user/Library/Application Support/iPhone Simulator/5.1/Applications/5403DF94-1B63-4CCF-8A5B-548ED5902DBE/hello.app
NSString *stylePath = [NSString stringWithFormat:@"%@/FALog.css",[[NSBundle mainBundle] resourcePath]];
NSString *styleStr = [NSString stringWithContentsOfFile:stylePath encoding:NSUTF8StringEncoding error:nil];
// //NSLog(@"styleStr:%@",styleStr);
str = [NSString stringWithFormat:@"%@ \n%@",styleStr,str];
}
NSDictionary * attributes = [manager attributesOfItemAtPath:path error:nil];
long long fileSize = [[attributes objectForKey:NSFileSize] longLongValue];
NSFileHandle *uFile = [NSFileHandle fileHandleForWritingAtPath:path];
[uFile seekToFileOffset:fileSize];
[uFile writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
-----得到file 大小
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
============================================================
-------文件的创建,遍历,得到文件属性(创建日期等),删除等操作
直接从工程里面扒出来的,不过写的很清晰。
NSDateFormatter * dateFormatter = [[[NSDateFormatter alloc] init]autorelease];
[dateFormatter setDateFormat:@"yyyy_MM_dd"];
NSString* dataStr = [dateFormatter stringFromDate:[NSDate date]];
NSString *dicpath = [NSString stringWithFormat:@"%@/Documents/logs",NSHomeDirectory()];
NSString *path = [NSString stringWithFormat:@"%@/log%@.html",dicpath,dataStr];
NSFileManager *manager = [NSFileManager defaultManager];
//如果不存在当天的日志,则新建
if ([manager fileExistsAtPath:path] == NO) {
//删除max interval 以上的log
NSFileManager* fm= [[[NSFileManager alloc] init]autorelease];
NSArray *levelList = [fm contentsOfDirectoryAtPath:dicpath error:nil ] ;
for (NSString *fname in levelList) {
NSString *fpath = [NSString stringWithFormat:@"%@/%@",dicpath,fname];
NSDictionary *fileAttributes = [manager attributesOfItemAtPath:fpath error:nil];
NSDate * creationDate=nil;
if ((creationDate = [fileAttributes objectForKey:NSFileCreationDate])) {
NSTimeInterval interval = [creationDate timeIntervalSinceNow];
//printf("%s's interval is: %f\n",[fpath UTF8String ],interval);
if ((interval*-1) >MAX_INTERVAL*24*60*60) {
[manager removeItemAtPath:fpath error:nil];
//NSLog(@"delete file:%@ \n",fpath );
}
}
}
//创建新log
[manager createDirectoryAtPath:dicpath withIntermediateDirectories:YES attributes:nil error:nil];
[manager createFileAtPath:path contents:nil attributes:nil];
///Users/user/Library/Application Support/iPhone Simulator/5.1/Applications/5403DF94-1B63-4CCF-8A5B-548ED5902DBE/hello.app
NSString *stylePath = [NSString stringWithFormat:@"%@/FALog.css",[[NSBundle mainBundle] resourcePath]];
NSString *styleStr = [NSString stringWithContentsOfFile:stylePath encoding:NSUTF8StringEncoding error:nil];
// //NSLog(@"styleStr:%@",styleStr);
str = [NSString stringWithFormat:@"%@ \n%@",styleStr,str];
}
NSDictionary * attributes = [manager attributesOfItemAtPath:path error:nil];
long long fileSize = [[attributes objectForKey:NSFileSize] longLongValue];
NSFileHandle *uFile = [NSFileHandle fileHandleForWritingAtPath:path];
[uFile seekToFileOffset:fileSize];
[uFile writeData:[str dataUsingEncoding:NSUTF8StringEncoding]];
-----得到file 大小
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];
NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];
相关文章推荐
- iOS中NSFileManager文件常用操作整合
- Hadoop HDFS文件常用操作及注意事项(更新)
- iOS 文件操作(NSFileManager)结合相册选取、保存、和读取
- Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作
- C++ 文件操作(不断更新ing)
- iOS 开发常用好用的第三方《不断更新》
- iOS 沙盒目录结构和文件操作(NSFileManager)(一)
- Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作
- iOS文件和目录操作,iOS文件操作,NSFileManager使用文件操作:
- iOS常用文件操作
- Linux 常用命令学习-文件及文件夹操作相关命令-持续更新
- IOS文件常用操作
- Categories IOS比较常用的第三方及实例(不断更新中),ios不断更新
- Objective-C文件和目录操作,IOS文件操作,NSFileManager使用文件操作
- Java 文件操作的学习(不断更新中)
- 常用sed操作(不断更新)
- linux的一些常用操作(随时更新)命令导入sql文件
- 文件管理类NSFileManager常用操作
- IOS开发 沙盒中的文件操作和文件系统NSFilemanager的操作