简体   繁体   English

获取给定路径的文件大小

[英]Get file size given a path

I'm new in objective-c. 我是Objective-C的新手。 I have a path to file contained in an NSString and I want get file size. 我有一个包含在NSString中的文件的路径,我想获取文件大小。 I found this example and change deprecated code with attributesOfItemAtPath:error: but path is always invalid. 我找到了此示例,并使用attributeOfItemAtPath:error更改了不建议使用的代码:但是路径始终无效。

NSFileManager *fileManager = [[NSFileManager alloc] init];
NSString *path = @"~/Library/Safari/History.plist";
NSDictionary *fileAttributes = [fileManager attributesOfItemAtPath: path error: NULL];


if (fileAttributes != nil) {
    NSNumber *fileSize;

    if (fileSize == [fileAttributes objectForKey:NSFileSize]) {
        NSLog(@"File size: %qi\n", [fileSize unsignedLongLongValue]);
    }

}
else {
    NSLog(@"Path (%@) is invalid.", pPath);
}
[NSFileManager release];

This should work: 这应该工作:

uint64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:_filePath error:nil] fileSize];

It's very similar to the one used by you, but in yours there's a mistake: you put NULL instead of nil in the error: handling. 这与您使用的方法非常相似,但是在您的方法中有一个错误:您在error: NULL而不是nil进行处理。

Make sure also to expand tilde in your path, as explained in the documentation : use stringByExpandingTildeInPath , so your NSString *path should be something like this: 确保还按照文档中的说明扩展路径中的波浪号:使用stringByExpandingTildeInPath ,因此您的NSString *path应该是这样的:

NSString *path = [[NSString stringWithString:@"~/Library/Safari/History.plist"] stringByExpandingTildeInPath];

Here you can find some explanations about the difference between nil and NULL . 在这里,您可以找到有关nilNULL之间差异的一些解释。

您可能需要使用以下方法扩展路径:

 - (NSString *)stringByExpandingTildeInPath

you can get size by : 您可以通过以下方式获得尺寸:

NSDictionary * properties = [[NSFileManager defaultManager] attributesOfItemAtPath:yourFilePath error:nil];
NSNumber * size = [properties objectForKey: NSFileSize];

size is a NSNumber that contains a unsigned long long. size是一个NSNumber,其中包含一个无符号的long long。

Your Path will always be invalid because of a super-silly bug in your code. 由于代码中的超级错误,您的路径将始终无效。

Change 更改

if (fileSize == [fileAttributes objectForKey:NSFileSize]) {

to

if (fileSize = [fileAttributes objectForKey:NSFileSize]) {

I hope no further explanatiuon would be required. 我希望不再需要进一步说明。

Use the defaultManager class method on NSFileManager instead of creating your own instance. defaultManager上使用defaultManager类方法,而不是创建自己的实例。 Also, do not include ~ (tilde) symbol in your file path. 另外,文件路径中请勿包含~ (波浪号)符号。 Use the NSHomeDirectory() function to get the home directory instead. 使用NSHomeDirectory()函数来获取主目录。 Here's an example: 这是一个例子:

NSString *path = [NSString stringWithFormat:@"%@/Library/Safari/History.plist", NSHomeDirectory()];
[[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil] fileSize];

This should return the size of your file. 这应该返回文件的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM