读书人

资料的长度究竟是算什么值

发布时间: 2013-09-08 15:21:21 作者: rapoo

文件的长度,究竟是算什么值?

File file = new File("newke.jpg");
InputStream in = null;
int length = 0;
in = new BufferedInputStream(new FileInputStream(file));
length = (int)file.length();


file.length() 可以得到一个长度。这里是图片,这样获取是文件长度是否正确??


如果是文本文件,那么这个长度是字符数,还是字节数呢?
[解决办法]
The length, in bytes, of the file denoted by this abstract pathname
字节
[解决办法]


/**
* Returns the length of the file denoted by this abstract pathname.
* The return value is unspecified if this pathname denotes a directory.
*
* @return The length, in bytes, of the file denoted by this abstract
* pathname, or <code>0L</code> if the file does not exist. Some
* operating systems may return <code>0L</code> for pathnames
* denoting system-dependent entities such as devices or pipes.
*
* @throws SecurityException
* If a security manager exists and its <code>{@link
* java.lang.SecurityManager#checkRead(java.lang.String)}</code>
* method denies read access to the file


*/
public long length() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkRead(path);
}
return fs.getLength(this);
}




/**
* Return the length in bytes of the file denoted by the given abstract
* pathname, or zero if it does not exist, is a directory, or some other
* I/O error occurs.
*/
public abstract long getLength(File f);


字节数,注释里面说明了。

读书人网 >J2SE开发

热点推荐