Extract top level class name cutting ".java" from the end. - Java Reflection

Java examples for Reflection:Class

Description

Extract top level class name cutting ".java" from the end.

Demo Code


//package com.java2s;
import java.io.File;

public class Main {
    public static void main(String[] argv) throws Exception {
        String sourceFile = "java2s.com";
        System.out.println(getTopLevelClassName(sourceFile));
    }//from  ww  w  .  ja  v  a2s .  c  o  m

    /**
     * Extract top level class name cutting ".java" from the end.
     *
     * @param   sourceFile
     *
     * @return
     */
    public static String getTopLevelClassName(final String sourceFile) {
        String className = sourceFile.substring(0, sourceFile.length() - 5); // strip ".java"
        return className.replace(File.separatorChar, '.');
    }
}

Related Tutorials