I'm trying to find a file size. But my length is equl to zero

余生颓废 提交于 2019-12-11 05:46:36

问题


I'm trying to find a file size.

 NSFileHandle *output = [NSFileHandle fileHandleForWritingAtPath:self.finalPath]; 
//seek to begin of the file
[output seekToFileOffset:0];
NSData *mydata = [output availableData];
NSLog(@"length: %d", [mydata length]);

But my length is equl to zero. Why?


回答1:


availableData is for reading files. If you just want to find out the size of a file, you don't actually have to open it. Just use NSFileManager like this:

NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self.finalPath error:NULL];
unsigned long long fileSize = [attributes fileSize]; // in bytes



回答2:


[fileHandler seekToEndOfFile], this returns the length of your file data, your code [output seekToFileOffset:0] means from 0-0,so there is no data



来源:https://stackoverflow.com/questions/9479824/im-trying-to-find-a-file-size-but-my-length-is-equl-to-zero

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!