计算文件夹文件大小
来源:互联网 发布:mac os u盘启动盘 编辑:程序博客网 时间:2024/05/22 23:58
http://www.iliunian.com/1392.html
计算文件夹size
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
-(long)fileSizeForDir:(NSString*)path//计算文件夹下文件的总大小
{
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];
for(int i = 0; i<[array count]; i++)
{
NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];
BOOL isDir;
if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) )
{
NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPatherror:nil];
size+= fileAttributeDic.fileSize;
}
else
{
[self fileSizeForDir:fullPath];
}
}
[fileManager release];
return size;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
1. NSFileManager 文件操作
常见的NSFileManager文件的方法:
-(BOOL)contentsAtPath:path 从文件中读取数据
-(BOOL)createFileAtPath:path contents:(BOOL)data attributes:attr 向一个文件写入数据
-(BOOL)removeFileAtPath: path handler: handler 删除一个文件
-(BOOL)movePath: from toPath: to handler: handler 重命名或移动一个文件(to可能已经存在)
-(BOOL)copyPath:from toPath:to handler: handler 复制文件 (to不能存在)
-(BOOL)contentsEqualAtPath:path1 andPath:path2 比较两个文件的内容
-(BOOL)fileExistsAtPath:path 测试文件是否存在
-(BOOL)isReadablefileAtPath:path 测试文件是否存在,且是否能执行读操作
-(BOOL)isWritablefileAtPath:path 测试文件是否存在,且是否能执行写操作
-(NSDictionary *)fileAttributesAtPath:path traverseLink:(BOOL)flag 获取文件的属性
-(BOOL)changeFileAttributes:attr atPath:path 更改文件的属性
NSFileManager对象的创建 :
NSFileManager *fm;
fm = [NSFileManager defaultManager];
NSDictionary *attr =[fm fileAttributesAtPath: fname traverseLink: NO] ; //文件属性
NSLog(@"file size is:%i bytes ",[[attr objectForKey:NSFileSize] intValue]);
NSData *data =[fm contentsAtPath:@"filename"];//文件内容
常见的NSFileManager目录的方法:
-(NSString *)currentDirectoryPath 获取当前目录
-(BOOL)changeCurrentDirectoryPath:path 更改当前目录
-(BOOL)copyPath:from toPath:to handler:handler 复制目录结构,to不能已经存在
-(BOOL)createDirectoryAtPath:path attributes:attr 创建目录
-(BOOL)fileExistsAtPath:path isDirectory:(BOOL *)flag 测试文件是否为目录 (flag存储结构yes/no)
-(NSArray *)contentsOfDirectoryAtPath:path 列出目录的内容
-(NSDirectoryEnumerator *)enumeratorAtPath:path 枚举目录的内容
-(BOOL)removeFileAtPath:path handler:handler 删除空目录
-(BOOL)movePath:from toPath:to handler:handler 重命名或移动一个目录,to不能是已经存在的
path= [fm currentDirectoryPath] ;
NSArray *dirarray;
NSDirectoryEnumerator *direnu;
direnu = [fm enumeratorAtPath:path];
NSLog(@"contents of %@\n",path);
BOOL flag;
while((path = [direnu nextObject])!=nil)
{
NSLog(@"%@ ",path);
[fm fileExistsAtPath:path isDirectory:&flag];
if(flag == YES)
[direnu skipDescendents]; //跳过子目录
}
path= [fm currentDirectoryPath] ;
dirarray = [fm contentsOfDirectoryAtPath:path];
NSLog(@"%@ ",dirarray);
常用路径工具函数
NSString * NSUserName(); 返回当前用户的登录名
NSString * NSFullUserName(); 返回当前用户的完整用户名
NSString * NSHomeDirectory(); 返回当前用户主目录的路径
NSString * NSHomeDirectoryForUser(); 返回用户user的主目录
NSString * NSTemporaryDirectory(); 返回可用于创建临时文件的路径目录
常用路径工具方法
-(NSString *) pathWithComponents:components 根据components中元素构造有效路径
-(NSArray *)pathComponents 析构路径,获取路径的各个部分
-(NSString *)lastPathComponent 提取路径的最后一个组成部分
-(NSString *)pathExtension 路径扩展名
-(NSString *)stringByAppendingPathComponent:path 将path添加到现有路径末尾
-(NSString *)stringByAppendingPathExtension:ext 将拓展名添加的路径最后一个组成部分
-(NSString *)stringByDeletingPathComponent 删除路径的最后一个部分
-(NSString *)stringByDeletingPathExtension 删除路径的最后一个部分 的扩展名
-(NSString *)stringByExpandingTildeInPath 将路径中的代字符扩展成用户主目录(~)或指定用户主目录(~user)
-(NSString *)stringByResolvingSymlinksInPath 尝试解析路径中的符号链接
-(NSString *)stringByStandardizingPath 通过尝试解析~、..、.、和符号链接来标准化路径
-
使用路径 NSPathUtilities.h
tempdir = NSTemporaryDirectory(); 临时文件的目录名
path = [fm currentDirectoryPath];
[path lastPathComponent]; 从路径中提取最后一个文件名
fullpath = [path stringByAppendingPathComponent:fname];将文件名附加到路劲的末尾
extenson = [fullpath pathExtension]; 路径名的文件扩展名
homedir = NSHomeDirectory();用户的主目录
component = [homedir pathComponents]; 路径的每个部分
NSProcessInfo类:允许你设置或检索正在运行的应用程序的各种类型信息
(NSProcessInfo *)processInfo 返回当前进程的信息
-(NSArray*)arguments 以NSString对象数字的形式返回当前进程的参数
-(NSDictionary *)environment 返回变量/值对词典。描述当前的环境变量
-(int)processIdentity 返回进程标识
-(NSString *)processName 返回进程名称
-(NSString *)globallyUniqueString 每次调用该方法都会返回不同的单值字符串,可以用这个字符串生成单值临时文件名
-(NSString *)hostname 返回主机系统的名称
-(unsigned int)operatingSystem 返回表示操作系统的数字
-(NSString *)operatingSystemName 返回操作系统名称
-(NSString *)operatingSystemVersionString 返回操作系统当前版本
-(void)setProcessName:(NSString *)name 将当前进程名称设置为name
过滤数组中的文件类型 : [fileList pathsMatchingExtensions:[NSArrayarrayWithObject:@"jpg"]];
///////////////////////////////////////////////////////////////////////////////////////////////////////////
2. NSFileHandle 基本文件操作
常用NSFileHandle方法
(NSFileHandle *)fileHandleForReadingAtPath:path 打开一个文件准备读取
(NSFileHandle *)fileHandleForWritingAtPath:path 打开一个文件准备写入
(NSFileHandle *)fileHandleForUpdatingAtPath:path 打开一个文件准备更新(读取和写入)
-(NSData *)availableData 从设备或通道返回可用数据
-(NSData *)readDataToEndOfFile 读取其余的数据直到文件末尾(最多UINT_MAX字节)
-(NSData *)readDataOfLength:(unsigned int)bytes 从文件读取指定数目bytes的内容
-(void)writeData:data 将data写入文件
-(unsigned long long) offsetInFile 获取当前文件的偏移量
-(void)seekToFileOffset:offset 设置当前文件的偏移量
-(unsigned long long) seekToEndOfFile 将当前文件的偏移量定位的文件末尾
-(void)truncateFileAtOffset:offset 将文件的长度设置为offset字节
-(void)closeFile 关闭文件
获取文件大小
Using the C FILE type:
int getFileSizeFromPath(char * path)
{
FILE * file;
int fileSizeBytes = 0;
file = fopen(path,"r");
if(file>0){
fseek(file, 0, SEEK_END);
fileSizeBytes = ftell(file);
fseek(file, 0, SEEK_SET);
fclose(file);
}
return fileSizeBytes;
}
or in XCode use the NSFileManager:
NSFileManager * filemanager = [[NSFileManager alloc]init];
if([filemanager fileExistsAtPath:[self getCompletePath] isDirectory:&isDirectory]){
NSDictionary * attributes = [filemanager attributesOfItemAtPath:[self getCompletePath] error:nil];
// file size
NSNumber *theFileSize;
if (theFileSize = [attributes objectForKey:NSFileSize])
_fileSize= [theFileSize intValue];
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
在头文件声明一个定量的文件夹路径
#define kDocument_Folder [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"]
以下方法是获取文件夹里面的文件数
NSFileManager *fM = [NSFileManager defaultManager];
NSArray *fileList = [fM contentsOfDirectoryAtPath:kDocument_Folder error:nil];
int filescount=[fileList count];
以下是获取文件夹里面的文件信息,还做了文件夹里文件总大小的计算
const unsigned int bytes = 1024*1024 ; //字节数,如果想获取KB就1024,MB就1024*1024
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setPositiveFormat:@"##0.00"];
float size;
NSString *path;
//遍历文件夹里面的文件
for(NSString *file in fileList) {
path = [kDocument_Folder stringByAppendingPathComponent:file];
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil];
size=size+(float)[fileAttributes fileSize];
}
NSNumber *total = [NSNumber numberWithFloat:(size/bytes)];
NSString *text=[NSString stringWithFormat:@"占用容量:%@ MB",[formatter stringFromNumber:total]];
在MAC10.5之前是使用
NSDictionary *fileAttributes = [[NSFileManager defaultManager] fileAttributesAtPath:path traverseLink:YES];这个方法,
但在这之后用attributesOfItemAtPath:error:替换。
- 计算文件夹文件大小
- 计算文件大小或者文件夹的大小
- android获取、计算文件夹和文件大小的方法
- 计算缓存,某个文件夹和某个文件大小的方法
- 计算文件大小
- 计算文件大小
- 计算文件大小
- 计算文件大小
- 计算文件大小
- Android计算文件夹大小、文件大小单位转换、删除文件夹及其内容
- 计算windows一个文件大小
- Hadoop计算文件大小
- C++计算文件大小
- 语音文件大小计算
- 视频文件大小计算
- VC计算大文件大小
- 图片格式及文件大小计算
- iOS_计算文件大小
- 在网页上显示用户控件上学选择的内容(例子省市选择器)
- The Cisco Three Layer Hierarchical Model vs the OSI Model
- 可以根据自己输入的地址下载网络上的图片
- C# DataTable 过滤重复数据
- 为oracle中表的数学添加唯一性约束
- 计算文件夹文件大小
- 06_标签
- 关于大于9的串口打开失败的原因
- spring中JUnit4测试中,AbstractTransactionalJUnit4SpringContextTests报错关于javax.sql.DataSource
- 可以根据自己输入的地址下载网页信息
- android实现从网上获取数据
- Web性能优化方案
- MyBatis动态SQL
- Android Intent 用法全面总结