Java File Name Create toFileName(String className)

Here you can find the source of toFileName(String className)

Description

Convert the Java-class-name (dot delimited package name)to the equivalent Java-class-file-name .

License

Open Source License

Parameter

Parameter Description
className The class name string to be translated.

Return

The file name representaion of the supplied runtime class String.

Declaration

public static String toFileName(String className) 

Method Source Code

//package com.java2s;
/*/*w  w w .  j  av  a  2 s  .  c  o  m*/
   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
*/

public class Main {
    /**
     * 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();
        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.length() > 6) {
            fileName.setLength(className.length() - 6);
        }
        for (int i = 0; i < fileName.length(); i++) {
            if (fileName.charAt(i) == '.') {
                fileName.setCharAt(i, '/');
            }
        }
        fileName.append(".class");

        return fileName.toString();
    }
}

Related

  1. generateFileName(String fileName)
  2. generateFileNameWithoutExtension(final String testClassName, final String testId, final boolean includeDate)
  3. getRandomFileName(@Nullable final String prefix)
  4. toFileName(final Object obj)
  5. toFileName(String address)
  6. toFilename(String filenameOrURI)
  7. toFilename(String key, final String suffix)
  8. toFileName(String moduleName)
  9. toFilename(String name, String extension)