IOS檔案常用操作 NSFileManager—-不斷更新中

來源:互聯網
上載者:User
============================================================博文原創,轉載請聲明出處電子咖啡-專註於移動互連網============================================================

-------檔案的建立,遍曆,得到檔案屬性(建立日期等),刪除等操作

直接從工程裡面扒出來的,不過寫的很清晰。

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];

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.