File f = new File("test.txt");
long len = f.length();

解决方案 »

  1.   

    /**
         * 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
         *
         * @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);
        }