转换Java

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

441
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
将Java类名称(点分隔的包名称)转换为等效的Java类文件名称。

/*

   Milyn - Copyright (C) 2006 - 2010


   This library is free software; you can redistribute it and/or

   modify it under the terms of the GNU Lesser General Public

   License (version 2.1) as published by the Free Software 

   Foundation.


   This library is distributed in the hope that it will be useful,

   but WITHOUT ANY WARRANTY; without even the implied warranty of

   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  

            

   See the GNU Lesser General Public License for more details:    

   http://www.gnu.org/licenses/lgpl.txt

 */

//package com.nowjava;


public class Main {/* from N o  w  J a v a . c o m - 时  代  Java*/

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

        String className = "nowjava.com";

        System.out.println(toFileName(className));

    }


    /**

     * Convert the Java-class-name (dot delimited package name)to the 

     * equivalent Java-class-file-name .

     * <p/>

     * EG:<br/>

     * a.b.c.X converts to a/b/c/X.class<br/>

     * a.b.c.X.class converts to a/b/c/X.class<br/>

     * a/b/c/X.class converts to a/b/c/X.class<br/>

     * a/b/c/X converts to a/b/c/X.class<br/>

     * @param className The class name string to be translated.

     * @return The file name representaion of the supplied runtime class String.

     */

    public static String toFileName(String className) {

        StringBuffer fileName;


        if (className == null) {

            throw new IllegalArgumentException(

                    "null 'className' arg in method call.");

        }

        className = className.trim();/**来 自 时 代 J a v a - N o w J a v a . c o m**/

        if (className.equals("")) {

            throw new IllegalArgumentException(

                    "empty 'className' arg in method call.");

        }


        fileName = new StringBuffer(className);

        // Fixup the name - replace '.' with '/' and append ".class" ( possibly 

        // after already removing it - to avoid it from being converted 

        // to "/class").

        if (className.endsWith(".class") && className.l
展开阅读全文