首页 > 其它 > java 得到运行时行号
2016
02-16

java 得到运行时行号

public class TestClass  
{  
    public static void main(String[] args)  
    {  
        test();  
    }  
  
    public static void test()  
    {  
        getCaller();  
    }  
  
    public static void getCaller()  
    {  
        StackTraceElement stack[] = (new Throwable()).getStackTrace();  
        for (int i = 0; i < stack.length; i++)  
        {  
            StackTraceElement s = stack[i];  
            System.out.format(" ClassName:%d\t%s\n", i, s.getClassName());  
            System.out.format("MethodName:%d\t%s\n", i, s.getMethodName());  
            System.out.format("  FileName:%d\t%s\n", i, s.getFileName());  
            System.out.format("LineNumber:%d\t%s\n\n", i, s.getLineNumber());  
        }  
    }  
    
}

 

最后编辑:
作者:第五维
这个作者貌似有点懒,什么都没有留下。