StackTraceElement.getFileName() Method



Description

The java.lang.StackTraceElement.getFileName() method returns the name of the source file containing the execution point represented by this stack trace element.

Declaration

Following is the declaration for java.lang.StackTraceElement.getFileName() method

public String getFileName()

Parameters

NA

Return Value

This method returns the name of the file containing the execution point represented by this stack trace element, or null if this information is unavailable.

Exception

NA

Example

The following example shows the usage of java.lang.StackTraceElement.getFileName() method.

package com.tutorialspoint;

import java.lang.*;

public class StackTraceElementDemo {

   public static void main(String[] args) {
      function1();
   }
 
   public static void function1() {
      new StackTraceElementDemo().function2();
   }
 
   public void function2() {
      System.out.print("file name : ");
      System.out.print(Thread.currentThread().getStackTrace()[1].
      getFileName());
   }
}   

Let us compile and run the above program, this will produce the following result −

file name : StackTraceElementDemo.java
java_lang_stacktraceelement.htm
Advertisements