Java开发常用知识

来源:互联网 发布:淘宝客代理分佣系统 编辑:程序博客网 时间:2024/05/22 10:36

getClass()和super.getClass()

getClass().getName() 和super.getClass().getName()的结果是不是一样呢?答案是一样的。
super并没有代表超类的一个引用的能力,只是代表调用父类的方法而已。所以,在子类的方法中,不能这样用System.out.println(super);也不能使用super.super.method()。
super.getClass()只是表示调用父类的方法而已。getClass方法来自Object类,它返回对象在运行时的类型。
如果想获得父类的Class,请用getClass().getSuperclass().getName()
具体请看源码注释:

    /**     * Returns the unique instance of {@link Class} that represents this     * object's class. Note that {@code getClass()} is a special case in that it     * actually returns {@code Class<? extends Foo>} where {@code Foo} is the     * erasure of the type of the expression {@code getClass()} was called upon.     * <p>     * As an example, the following code actually compiles, although one might     * think it shouldn't:     * <p>     * <pre>{@code     *   List<Integer> l = new ArrayList<Integer>();     *   Class<? extends List> c = l.getClass();}</pre>     *     * @return this object's {@code Class} instance.     */    public final Class<?> getClass() {      return shadow$_klass_;    }
    /**     * Returns the {@code Class} object which represents the superclass of the     * class represented by this {@code Class}. If this {@code Class} represents     * the {@code Object} class, a primitive type, an interface or void then the     * method returns {@code null}. If this {@code Class} represents an array     * class then the {@code Object} class is returned.     */    public Class<? super T> getSuperclass() {      // For interfaces superClass is Object (which agrees with the JNI spec)      // but not with the expected behavior here.      if (isInterface()) {        return null;      } else {        return superClass;      }    }

使用FileChannel copy文件

    /**     *      * copy file     *      * @param src     *            source file     * @param dest     *            target file     * @throws IOException     */    public static void copyFile(File src, File dest) throws IOException {        FileChannel inChannel = null;        FileChannel outChannel = null;        try {            if (!dest.exists()) {                dest.createNewFile();            }            inChannel = new FileInputStream(src).getChannel();            outChannel = new FileOutputStream(dest).getChannel();            inChannel.transferTo(0, inChannel.size(), outChannel);        } finally {            if (inChannel != null) {                inChannel.close();            }            if (outChannel != null) {                outChannel.close();            }        }    }

删除文件

    /**     * delete file     *      * @param file     *            file     * @return true if delete success     */    public static boolean deleteFile(File file) {        if (!file.exists()) {            return true;        }        if (file.isDirectory()) {            File[] files = file.listFiles();            for (File f : files) {                deleteFile(f);            }        }        return file.delete();    }

获取文件MD5值

    /**     * get md5     *      * @param file     * @return     */    public static String getFileMD5(File file) {        if (!file.isFile()) {            return null;        }        MessageDigest digest = null;        FileInputStream in = null;        byte buffer[] = new byte[8192];        int len;        try {            digest = MessageDigest.getInstance("MD5");            in = new FileInputStream(file);            while ((len = in.read(buffer)) != -1) {                digest.update(buffer, 0, len);            }        } catch (Exception e) {            Log.e(TAG, "getFileMD5", e);            return null;        } finally {            try {                if (in != null)                    in.close();            } catch (IOException e) {                Log.e(TAG, "getFileMD5", e);            }        }        BigInteger bigInt = new BigInteger(digest.digest());        return bigInt.toString();    }
0 0
原创粉丝点击