0

これを行う簡単な方法があると確信していますが、覚えていません。

Objective-Cで特定のファイルが空かどうかを確認するにはどうすればよいですか?できれば、その内容をロードせずに(ファイルが空でない場合に備えて)。

考えまし[[NSData dataWithContentsOfFile: PATH] length]たが、ファイルの大きさを知るためだけにファイル全体が読み込まれます。もっと効率的な方法はありませんか?

4

1 に答える 1

4

If by "empty" you mean it has absolutely nothing in it (0 bytes) you could do something like this:

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.
    }
}
于 2012-10-31T14:23:15.897 に答える