2012-10-31 110 views
0

我确定有这样一个简单的方法,但我不记得任何。检查文件是否为空

如何使用Objective-C检查某个文件是否为空?最好不加载其内容(以防文件不为空)。

我想到了[[NSData dataWithContentsOfFile: PATH] length],但加载整个文件只是为了知道它有多大。没有更有效的方法吗?

回答

4

如果“空”你的意思是它在它(0字节)绝对没有,你可以做这样的事情:

NSFileManager *manager = [NSFileManager defaultManager]; 
if ([manager fileExistsAtPath:path]) { 
    NSDictionary *attributes = [manager attributesOfItemAtPath:path error:nil]; 
    unsigned long long size = [attributes fileSize]; 
    if (attributes && size == 0) { 
     // file exists, but is empty. 
    } 
} 
+0

感谢,这正是我想要的。 – Alex