Java編程實(shí)現(xiàn)獲取當(dāng)前代碼行行號的方法示例
本文實(shí)例講述了Java編程實(shí)現(xiàn)獲取當(dāng)前代碼行行號的方法。分享給大家供大家參考,具體如下:
最近的項(xiàng)目中,為了實(shí)現(xiàn)自定義的log類,能夠輸出具體的代碼行行號,我通過使用StackTraceElement對象實(shí)現(xiàn)了。
具體內(nèi)容請參考下面的Demo代碼。這里指出需要注意的幾個問題:
1. 程序中返回的代碼行行號,是新建StackTrackElement對象的那一行。
2. 可以通過傳參的方法實(shí)現(xiàn)輸出特定行行號。
具體實(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緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。
- 利用Java獲取文件名、類名、方法名和行號的方法小結(jié)
- 基于JAVA代碼 獲取手機(jī)基本信息(本機(jī)號碼,SDK版本,系統(tǒng)版本,手機(jī)型號)
- java正則表達(dá)式的應(yīng)用 java讀取文件并獲取電話號碼
- java 用遞歸獲取一個目錄下的所有文件路徑的小例子
- java獲取系統(tǒng)路徑字體、得到某個目錄下的所有文件名、獲取當(dāng)前路徑
- java獲取properties屬性文件示例
- Java中獲取文件大小的詳解及實(shí)例代碼
- java字符串切割實(shí)例學(xué)習(xí)(獲取文件名)
- Java獲取文件的類型和擴(kuò)展名的實(shí)現(xiàn)方法
- 詳解Java如何獲取文件編碼格式
- java 獲取已知文件擴(kuò)展名的代碼
相關(guān)文章
Java微服務(wù)分布式調(diào)度Elastic-job環(huán)境搭建及配置
Elastic-Job在配置中提供了JobEventConfiguration,支持?jǐn)?shù)據(jù)庫方式配置,會在數(shù)據(jù)庫中自動創(chuàng)建JOB_EXECUTION_LOG和JOB_STATUS_TRACE_LOG兩張表以及若干索引,來記錄作業(yè)的相關(guān)信息2023-02-02spring-boot項(xiàng)目啟動遲緩異常排查解決記錄
這篇文章主要為大家介紹了spring-boot項(xiàng)目啟動遲緩異常排查解決記錄,突然在本地啟動不起來了,表象特征就是在本地IDEA上運(yùn)行時,進(jìn)程卡住也不退出,應(yīng)用啟動時加載相關(guān)組件的日志也不輸出2022-02-02Java ThreadLocal詳解_動力節(jié)點(diǎn)Java學(xué)院整理
ThreadLocal,很多地方叫做線程本地變量,也有些地方叫做線程本地存儲,本文會詳細(xì)的介紹一下,有興趣的可以了解一下2017-06-06Java集合中的CopyOnWriteArrayList使用詳解
這篇文章主要介紹了Java集合中的CopyOnWriteArrayList使用詳解,CopyOnWriteArrayList是ArrayList的線程安全版本,從他的名字可以推測,CopyOnWriteArrayList是在有寫操作的時候會copy一份數(shù)據(jù),然后寫完再設(shè)置成新的數(shù)據(jù),需要的朋友可以參考下2023-12-12Spring中的FactoryBean與ObjectFactory詳解
這篇文章主要介紹了Spring中的FactoryBean與ObjectFactory詳解,FactoryBean是一種特殊的bean,本身又是個工廠,實(shí)現(xiàn)了FactoryBean的bean會被注冊到容器中,需要的朋友可以參考下2023-12-12