获取资源的大小 [英] Get the size of a resource
问题描述
我正在使用 getClass().getResourceAsStream(path)
从捆绑资源中读取.
I'm using getClass().getResourceAsStream(path)
to read from bundle resources.
在读取整个流之前,我如何知道文件大小?
How can I know the file size before reading the entire stream?
打包后,我无法使用 getClass().getResource(path).toURI()
来访问它们,因此将无法正常工作.
I can't access them with getClass().getResource(path).toURI()
when it's packaged, so that won't work.
推荐答案
自然地,您无法查询流的总大小,因为根据定义,它不能使整个文件可用.想想将手伸到河里吧.您有一条小溪,但是您必须把整条河都掌握在手中,才能知道总流量.
Naturally you cannot query the total size of a stream, since by definition it does not make the whole file available to you. Think of popping your hand in a river. You have a piece of the stream,but you have to put the entire river in your hand to know the total volume.
在这种情况下,您必须将整个文件作为流读取并计算大小.请记住,您正在处理的类和资源可能是JAR文件或其他类型的压缩资源的一部分.在这种情况下,类加载器不必为资源提供文件句柄
In this case, you have to read the entire file as a stream and count the size. Remember you are dealing with classes and resources that may be part of a JAR file or other kind of compressed resource. The classloader does not have to provide a file handle to the resource in this case
这篇关于获取资源的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!