一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Java編程實現獲取當前代碼行行號的方法示例

Java編程實現獲取當前代碼行行號的方法示例

2020-12-17 14:41念月思靈 Java教程

這篇文章主要介紹了Java編程實現獲取當前代碼行行號的方法,結合實例形式分析了java基于StackTraceElement對象實現獲取代碼行號的相關操作技巧,需要的朋友可以參考下

本文實例講述了Java編程實現獲取當前代碼行行號的方法。分享給大家供大家參考,具體如下:

最近的項目中,為了實現自定義的log類,能夠輸出具體的代碼行行號,我通過使用StackTraceElement對象實現了。

具體內容請參考下面的Demo代碼。這里指出需要注意的幾個問題:

1. 程序中返回的代碼行行號,是新建StackTrackElement對象的那一行。
2. 可以通過傳參的方法實現輸出特定行行號。

具體實現代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/**
*
*/
package leo.demo.training;
/**
* Get current java file name and current code line number
* @author Leo Xie
*/
public class CurrentLine {
  /**
  * @param args
  */
  public static void main(String[] args) {
    StackTraceElement ste1 = null;
    // get current thread and its related stack trace
    StackTraceElement[] steArray = Thread.currentThread().getStackTrace();
    int steArrayLength = steArray.length;
    String s = null;
    // output all related info of the existing stack traces
    if(steArrayLength==0) {
      System.err.println("No Stack Trace.");
    } else {
      for (int i=0; i<steArrayLength; i++) {
        System.out.println("Stack Trace-" + i);
        ste1 = steArray[i];
        s = ste1.getFileName() + ": Line " + ste1.getLineNumber();
        System.out.println(s);
      }
    }
    // the following code segment will output the line number of the "new " clause
    // that's to say the line number of "StackTraceElement ste2 = new Throwable().getStackTrace()[0];"
    StackTraceElement ste2 = new Throwable().getStackTrace()[0];
    System.out.println(ste2.getFileName() + ": Line " + ste2.getLineNumber());
    // the following clause will output the line number in the external method "getLineInfo()"
    System.out.println(getLineInfo());
    // the following clause will output its current line number
    System.out.println(getLineInfo(new Throwable().getStackTrace()[0]));
  }
  /**
  * return current java file name and code line number
  * @return String
  */
  public static String getLineInfo() {
    StackTraceElement ste3 = new Throwable().getStackTrace()[0];
    return (ste3.getFileName() + ": Line " + ste3.getLineNumber());
  }
  /**
  * return current java file name and code line name
  * @return String
  */
  public static String getLineInfo(StackTraceElement ste4) {
    return (ste4.getFileName() + ": Line " + (ste4.getLineNumber()));
  }
}

希望本文所述對大家java程序設計有所幫助。

原文鏈接:http://www.cnblogs.com/xxpal/articles/1209378.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩中文字幕视频在线观看 | 精品国产品在线18年 | 亚洲欧美日韩精品 | 欧美在线一级片 | 爱情岛论坛亚洲一号路线 | 韩国免费特一级毛片 | 国产亚洲精品九九久在线观看 | 1024国产高清精品推荐 | 奇米影视4444| 亚洲国产成人久久综合一 | 午夜理论片日本中文在线 | 人体欣赏孕妇季玥图片 | 精品国产一区二区在线观看 | 被强迫变性翘秘书 | 免费网站国产 | 亚洲 欧美 中文字幕 在线 | 免费看日产一区二区三区 | 亚洲精美视频 | 爱情岛论坛亚洲品质自拍视频 | 精品亚洲欧美中文字幕在线看 | 国产精品视频久久 | 狠狠色婷婷 | 视频免费视频观看网站 | 无限资源在线观看播放 | 大陆男男gayxxxxvideo | 亚洲国产精品久久精品怡红院 | 国产玖玖在线 | 性刺激欧美三级在线现看中文 | 日本在线一区二区 | 亚洲精品成人在线 | 四虎院影永久在线观看 | 日本艳鉧动漫1~6完整版在 | chinese男gay飞机同志 | 500福利第一巨人导航 | 亚洲成人一区在线 | 男人的私人影院 | 嫩交18xxxx | 纲手被强喷水羞羞漫画 | 色综合中文字幕天天在线 | 欧洲一级 | 国产免费好大好硬视频 |