【问题标题】:Finding file's size查找文件大小
【发布时间】:2011-08-10 06:34:33
【问题描述】:

在我的 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];

【问题讨论】:

  • 奇怪,用你的代码,我可以正确获取文件大小,是什么问题?

标签: objective-c cocoa-touch ios nsfilemanager filesize


【解决方案1】:

试试这个;

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

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

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

【讨论】:

  • Xcode 4.5+ 单行:long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil][NSFileSize] longLongValue].
  • 最好是unsigned long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil][NSFileSize] unsignedLongLongValue],因为NSFileSize is an unsigned long long
  • 可以写得更短:unsigned long long size = [[NSFileManager defaultManager] attributesOfItemAtPath:path error:nil].fileSize
  • 您不能将 URL 作为 Path 参数发送。您需要发送[URL path]
【解决方案2】:

Swift 中的一个班轮:

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

【讨论】:

  • 为 Swift 3 更新:let fileSize = try (FileManager.default.attributesOfItem(atPath: url.path)[.size] as! NSNumber).uint64Value let fileSize = try (FileManager.default.attributesOfItem(atPath: url.path) as NSDictionary).fileSize()
  • 使用 Swift 错误处理....let fileSize = (try? FileManager.default.attributesOfItem(atPath: url.path))?[.size] as? Int ?? 0
【解决方案3】:

如果您有URLNSURL,而不是String),则可以在没有FileManager 的情况下获取文件大小:

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

【讨论】:

    【解决方案4】:

    Swift 4.x

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

    【讨论】:

      【解决方案5】:

      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
      }
      

      【讨论】:

        猜你喜欢
        • 2017-12-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-20
        • 2012-01-29
        • 2015-07-09
        • 1970-01-01
        相关资源
        最近更新 更多