文件的操作

来源:互联网 发布:js 获取跨域脚本错误 编辑:程序博客网 时间:2024/06/11 01:29

AppDelegate.m

AppDelegate.m     /*     在沙盒的Documents目录下创建一个文件wxhl.text,将文本“黯然”存入该文件,并且复制一个文件为wxhl_bak.text, 最后删除wxhl.text,并且计算文件的大小     */            NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/wxhl.text"];    NSFileManager *fileManager = [NSFileManager defaultManager];        NSString *str = @"黯然";        NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];        //1.创建文件    [fileManager createFileAtPath:filePath contents:data attributes:nil];        //2.复制文件    NSString *copyPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/wxhl_bak.text"];    [fileManager copyItemAtPath:filePath toPath:copyPath error:NULL];        //3.删除文件    if ([fileManager fileExistsAtPath:filePath]) {        [fileManager removeItemAtPath:filePath error:NULL];    }        //4.计算文件的大小    NSDictionary *attribute = [fileManager attributesOfItemAtPath:copyPath error:NULL];    NSLog(@"attribute:%@",attribute);        NSNumber *fileSize = [attribute objectForKey:NSFileSize];        long size = [fileSize longValue];    NSLog(@"文件:%ld 字节",size);    


0 0
原创粉丝点击