2009-05-02 37 views

Trả lời

73

Ghi nhớ rằng tệpAttributesAtPath: traverseLink: không còn được dùng như Mac OS X v10.5 nữa. Thay vào đó, hãy sử dụng attributesOfItemAtPath:error:, được mô tả ở số same URL đề cập đến thesamet.

Với sự báo trước rằng tôi là một newbie Objective-C, và tôi bỏ qua các lỗi có thể xảy ra trong việc kêu gọi attributesOfItemAtPath:error:, bạn có thể làm như sau:

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

Mã này rò rỉ Trình quản lý tệp được phân bổ. Tôi khuyên bạn chỉ cần sử dụng NSFileManager.defaultManager Singleton để tránh điều này. –

122

này lót có thể giúp mọi người:

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

Điều này trả về kích thước tệp theo byte.

+2

Tôi thích cái này. Nhưng cái này là gì? byte, Kb, v.v ...? Cảm ơn quá. – James

+6

byte - phép đo là byte –

+0

Ok, cảm ơn. Nhiều đánh giá cao. – James

10

CPU raises with attributesOfItemAtPath:error:
Bạn nên sử dụng 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); 
+0

Bạn có nên sử dụng tệpSystemRepresentation thay vì UTF8String tại đây không? –

+0

Bạn nói đúng. HFS + xác định phân tách Unicode chuẩn ("phân tích chuẩn") cho tên tệp. -UTF8String không đảm bảo trả lại thành phần phù hợp; -fileSystemRepresentation là. [1] (http://cocoadev.com/wiki/StringWithCString) –

+0

@ParagBafna Tôi biết đây là một chủ đề cũ nhưng bạn có biết làm thế nào tôi có thể sử dụng cấu trúc 'stat' nhanh? –

6

Tiếp theo câu trả lời từ Oded Bến Dov, tôi thà sử dụng một đối tượng ở đây:

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

Trong trường hợp một số người ta cần một phiên bản Swift:

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

Swift 2.2:

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

Nó sẽ cung cấp Kích thước tệp bằng Byte. ..

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

Nếu bạn muốn chỉ với kích thước tập tin byte chỉ cần sử dụng,

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

NSByteCountFormatter chuỗi chuyển filesize (từ Bytes) với KB chính xác, MB, GB ... lợi nhuận của nó như 120 MB hoặc 120 KB

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

Swift4:

 let attributes = try! FileManager.default.attributesOfItem(atPath: path) 
     let fileSize = attributes[.size] as! NSNumber 
Các vấn đề liên quan