转换Java

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

600
将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

 *//*来自 nowjava.com - 时  代  Java*/

//package com.nowjava;


public class Main {

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

        String fileName = "nowjava.com";

        System.out.println(toClassName(fileName));

    }


    /**

     * Convert the Java-class-file-name to the equivalent Java-class-name (dot 

     * delimited package name).

     * <p/>

     * EG:<br/>

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

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

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

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

     * @param fileName The file name String to be translated.

     * @return Java Class runtime name representation of the supplied file name String.

     */

    public static String toClassName(String fileName) {

        StringBuffer className;


        if (fileName == null) {

            throw new IllegalArgumentException(

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

        }

        fileName = fileName.trim();//来 自 nowjava - 时代Java

        if (fileName.equals("")) {

            throw new IllegalArgumentException(

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

        }


        className = new StringBuffer(fileName);

        // Fixup the name - replace '/' with '.' and remove ".class" if

        // present.

        if (fileName.endsWith(".class") 
展开阅读全文