集册 Java实例教程 将指定类的包名称获取为指定名称,并替换所有“。”

将指定类的包名称获取为指定名称,并替换所有“。”

欢马劈雪     最近更新时间:2020-01-02 10:19:05

501
将指定类的包名称获取为指定名称,并替换所有“。”

/*

 * Copyright (c) 2015-2016 QuartzDesk.com.

 * Licensed under the MIT license (https://opensource.org/licenses/MIT).

 *//*来自 时   代     Java  公  众  号 - nowjava.com*/

//package com.nowjava;


public class Main {

    public static void main(String[] argv) throws Exception {

        String name = "nowjava.com";

        Class clazz = String.class;

        System.out.println(getAbsoluteResourceName(name, clazz));

    }


    /**

     * Simply prepends the package name of the specified class to the

     * specified name and replaces all '.' with '/'.

     *

     * @param name a resource name.

     * @param clazz a class to be used to resolve the name if it

     * is a relative name.

     * @return the absolute resource name.

     */

    public static String getAbsoluteResourceName(String name, Class<?> clazz) {

        StringBuilder absName = new StringBuilder(clazz.getPackage()

         
展开阅读全文