objective-c 如何获取给定路径的文件大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/815471/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:28:53  来源:igfitidea点击:

How to get the file size given a path?

objective-ccocoamacos

提问by hekevintran

I have a path to file contained in an NSString. Is there a method to get its file size?

我有一个包含在 NSString 中的文件路径。有没有办法获得它的文件大小?

回答by Oded Ben Dov

This one liner can help people:

这种衬垫可以帮助人们:

unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:nil] fileSize];

This returns the file size in Bytes.

这将返回以字节为单位的文件大小。

回答by Frank Shearar

Bear in mind that fileAttributesAtPath:traverseLink: is deprecated as of Mac OS X v10.5. Use attributesOfItemAtPath:error:instead, described at the same URLthesamet mentions.

请记住,fileAttributesAtPath:traverseLink: 自 Mac OS X v10.5 起已弃用。attributesOfItemAtPath:error:改为使用,在相同的 URL 中描述。

With the caveat that I'm an Objective-C newbie, and I'm ignoring errors that might occur in calling attributesOfItemAtPath:error:, you can do the following:

需要注意的是,我是一个 Objective-C 新手,并且我忽略了调用中可能发生的错误attributesOfItemAtPath:error:,您可以执行以下操作:

NSString *yourPath = @"Whatever.txt";
NSFileManager *man = [NSFileManager defaultManager];
NSDictionary *attrs = [man attributesOfItemAtPath: yourPath error: NULL];
UInt32 result = [attrs fileSize];

回答by Tyler Long

In case some one needs a Swift version:

如果有人需要 Swift 版本:

let attr: NSDictionary = try! NSFileManager.defaultManager().attributesOfItemAtPath(path)
print(attr.fileSize())

回答by Parag Bafna

CPU raises with attributesOfItemAtPath:error:
You should use stat.

CPU 使用 attributesOfItemAtPath:error:
你应该使用stat

#import <sys/stat.h>

struct stat stat1;
if( stat([inFilePath fileSystemRepresentation], &stat1) ) {
      // something is wrong
}
long long size = stat1.st_size;
printf("Size: %lld\n", stat1.st_size);

回答by Sk Borhan Uddin

If you want only file size with bytes just use,

如果你只想使用字节的文件大小,

unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:yourAssetPath error:nil] fileSize];

NSByteCountFormatterstring conversion of filesize (from Bytes) with precise KB, MB, GB ... Its returns like 120 MBor 120 KB

NSByteCountFormatter文件大小(从字节)的字符串转换与精确的 KB、MB、GB ......它的返回像120 MB120 KB

NSError *error = nil;
NSDictionary *attrs = [[NSFileManager defaultManager] attributesOfItemAtPath:yourAssetPath error:&error];
if (attrs) {
    NSString *string = [NSByteCountFormatter stringFromByteCount:fileSize countStyle:NSByteCountFormatterCountStyleBinary];
    NSLog(@"%@", string);
}

回答by Apollo

Following the answer from Oded Ben Dov, I would rather use an object here:

按照 Oded Ben Dov 的回答,我宁愿在这里使用一个对象:

NSNumber * mySize = [NSNumber numberWithUnsignedLongLong:[[[NSFileManager defaultManager] attributesOfItemAtPath:someFilePath error:nil] fileSize]];

回答by Bill Chan

Swift 2.2:

斯威夫特 2.2:

do {
    let attr: NSDictionary = try NSFileManager.defaultManager().attributesOfItemAtPath(path)
    print(attr.fileSize())
} catch {
        print(error)
}

回答by Akshay Phulare

It will give File size in Byte...

它将以字节为单位给出文件大小...

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

回答by Denis Kreshikhin

Swift4:

斯威夫特4:

        let attributes = try! FileManager.default.attributesOfItem(atPath: path)
        let fileSize = attributes[.size] as! NSNumber

回答by d0ping

In Swift 3.x and above you can use:

在 Swift 3.x 及更高版本中,您可以使用:

do {
    //return [FileAttributeKey : Any]
    let attr = try FileManager.default.attributesOfItem(atPath: filePath)
    fileSize = attr[FileAttributeKey.size] as! UInt64

    //or you can convert to NSDictionary, then get file size old way as well.
    let attrDict: NSDictionary = try FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary
    fileSize = dict.fileSize()
} catch {
    print("Error: \(error)")
}