Java編程實(shí)現(xiàn)獲取當(dāng)前代碼行行號(hào)的方法示例
本文實(shí)例講述了Java編程實(shí)現(xiàn)獲取當(dāng)前代碼行行號(hào)的方法。分享給大家供大家參考,具體如下:
最近的項(xiàng)目中,為了實(shí)現(xiàn)自定義的log類,能夠輸出具體的代碼行行號(hào),我通過使用StackTraceElement對(duì)象實(shí)現(xiàn)了。
具體內(nèi)容請(qǐng)參考下面的Demo代碼。這里指出需要注意的幾個(gè)問題:
1. 程序中返回的代碼行行號(hào),是新建StackTrackElement對(duì)象的那一行。
2. 可以通過傳參的方法實(shí)現(xiàn)輸出特定行行號(hào)。
具體實(shí)現(xiàn)代碼:
/**
*
*/
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()));
}
}
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- 利用Java獲取文件名、類名、方法名和行號(hào)的方法小結(jié)
- 基于JAVA代碼 獲取手機(jī)基本信息(本機(jī)號(hào)碼,SDK版本,系統(tǒng)版本,手機(jī)型號(hào))
- java正則表達(dá)式的應(yīng)用 java讀取文件并獲取電話號(hào)碼
- java 用遞歸獲取一個(gè)目錄下的所有文件路徑的小例子
- java獲取系統(tǒng)路徑字體、得到某個(gè)目錄下的所有文件名、獲取當(dāng)前路徑
- java獲取properties屬性文件示例
- Java中獲取文件大小的詳解及實(shí)例代碼
- java字符串切割實(shí)例學(xué)習(xí)(獲取文件名)
- Java獲取文件的類型和擴(kuò)展名的實(shí)現(xiàn)方法
- 詳解Java如何獲取文件編碼格式
- java 獲取已知文件擴(kuò)展名的代碼
相關(guān)文章
Redis?command?timed?out兩種異常情況的解決方式
Redis是我們開發(fā)中常用的數(shù)據(jù)庫,下面這篇文章主要給大家介紹了關(guān)于Redis?command?timed?out兩種異常情況的解決方式,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-04-04
java密鑰交換算法DH定義與應(yīng)用實(shí)例分析
這篇文章主要介紹了java密鑰交換算法DH定義與應(yīng)用,結(jié)合實(shí)例形式分析了Java密鑰交換算法DH的原理、定義、使用方法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-09-09
SpringBoot項(xiàng)目中使用緩存Cache的正確方法分享
緩存可以通過將經(jīng)常訪問的數(shù)據(jù)存儲(chǔ)在內(nèi)存中,減少底層數(shù)據(jù)源如數(shù)據(jù)庫的壓力,從而有效提高系統(tǒng)的性能和穩(wěn)定性。本文就來講講SpringBoot項(xiàng)目中使用緩存Cache的正確姿勢(shì)吧2023-04-04
Mybatis Criteria使用and和or進(jìn)行聯(lián)合條件查詢的操作方法
這篇文章主要介紹了Mybatis Criteria的and和or進(jìn)行聯(lián)合條件查詢的方法,本文通過例子給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10
詳解如何使用Jersey客戶端請(qǐng)求Spring Boot(RESTFul)服務(wù)
本篇文章主要介紹了詳解如何使用Jersey客戶端請(qǐng)求Spring Boot(RESTFul)服務(wù),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
Java Calendar類的使用總結(jié)實(shí)例
這篇文章主要介紹了Java Calendar類的使用總結(jié)實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03

