查找文件大小

发布于 2024-11-02 16:57:23 字数 549 浏览 5 评论 0原文

在我的 iPhone 应用程序中,我使用以下代码来查找文件的大小。即使文件存在,我看到的大小为零。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *URL = [documentsDirectory stringByAppendingPathComponent:@"XML/Extras/Approval.xml"];

NSLog(@"URL:%@", URL);
NSError *attributesError = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];

int fileSize = [fileAttributes fileSize];

In my iPhone app I am using the following code to find a file's size. Even though the file exists, I am seeing zero for the size.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *URL = [documentsDirectory stringByAppendingPathComponent:@"XML/Extras/Approval.xml"];

NSLog(@"URL:%@", URL);
NSError *attributesError = nil;
NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];

int fileSize = [fileAttributes fileSize];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

骄兵必败 2024-11-09 16:57:23

试试这个;

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];

NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];

请注意,fileSize 不一定适合整数(尤其是带符号的整数),但对于 iOS,您当然可以将其降至 long,因为实际上您永远不会超过该值。该示例使用 long long,因为在我的代码中我必须与具有更大可用存储空间的系统兼容。

Try this;

NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:URL error:&attributesError];

NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
long long fileSize = [fileSizeNumber longLongValue];

Note that the fileSize won't necessarily fit in an integer (especially a signed one) although you could certainly drop to a long for iOS as you'll never exceed that in reality. The example uses long long as in my code I have to be compatible with systems with much larger storage available.

迷荒 2024-11-09 16:57:23

Swift 中的一个班轮:

let fileSize = try! NSFileManager.defaultManager().attributesOfItemAtPath(fileURL.path!)[NSFileSize]!.longLongValue

One liner in Swift:

let fileSize = try! NSFileManager.defaultManager().attributesOfItemAtPath(fileURL.path!)[NSFileSize]!.longLongValue
无法言说的痛 2024-11-09 16:57:23

如果您有 URLNSURL,而不是 String),则无需使用 FileManager 即可获取文件大小:

 let attributes = try? myURL.resourceValues(forKeys: Set([.fileSizeKey]))
 let fileSize = attributes?.fileSize // Int?

If you have a URL (NSURL, not a String), you can get the file size without a FileManager:

 let attributes = try? myURL.resourceValues(forKeys: Set([.fileSizeKey]))
 let fileSize = attributes?.fileSize // Int?
自由如风 2024-11-09 16:57:23

斯威夫特 4.x

do {
    let fileSize = try (FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary).fileSize()
            print(fileSize)
    } catch let error {
            print(error)
    }

Swift 4.x

do {
    let fileSize = try (FileManager.default.attributesOfItem(atPath: filePath) as NSDictionary).fileSize()
            print(fileSize)
    } catch let error {
            print(error)
    }
岁月流歌 2024-11-09 16:57:23

获取文件大小(以 MB 为单位)
尝试此代码 swift

func getSizeOfFile(withPath path:String) -> UInt64?
{
    var totalSpace : UInt64?

    var dict : [FileAttributeKey : Any]?

    do {
        dict = try FileManager.default.attributesOfItem(atPath: path)
    } catch let error as NSError {
         print(error.localizedDescription)
    }

    if dict != nil {
        let fileSystemSizeInBytes = dict![FileAttributeKey.systemSize] as! NSNumber

        totalSpace = fileSystemSizeInBytes.uint64Value
        return (totalSpace!/1024)/1024
    }
    return nil
}

Get the file size in MB
Try This code for swift

func getSizeOfFile(withPath path:String) -> UInt64?
{
    var totalSpace : UInt64?

    var dict : [FileAttributeKey : Any]?

    do {
        dict = try FileManager.default.attributesOfItem(atPath: path)
    } catch let error as NSError {
         print(error.localizedDescription)
    }

    if dict != nil {
        let fileSystemSizeInBytes = dict![FileAttributeKey.systemSize] as! NSNumber

        totalSpace = fileSystemSizeInBytes.uint64Value
        return (totalSpace!/1024)/1024
    }
    return nil
}
橘和柠 2024-11-09 16:57:23

如果您有 NSURL,则无需使用 NSFileManager 即可使用 resourceValuesForKeys:error:

NSNumber *fileSize = [url resourceValuesForKeys:@[NSURLFileSizeKey] error:nil][NSURLFileSizeKey];
unsigned long long bytes = fileSize.unsignedLongLongValue;

(ObjC 答案灵感来自 kelin 的 Swift 答案)

If you have an NSURL, you can get the file size without an NSFileManager using resourceValuesForKeys:error::

NSNumber *fileSize = [url resourceValuesForKeys:@[NSURLFileSizeKey] error:nil][NSURLFileSizeKey];
unsigned long long bytes = fileSize.unsignedLongLongValue;

(ObjC answer inspired by kelin's Swift answer)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文