當前位置:首頁 » 編程語言 » java行號

java行號

發布時間: 2023-01-01 18:01:33

1. java代碼中如何獲文件名和行號等源碼信息

Java是否提供某種方法:可以讓用戶代碼在編譯時確定源碼行號等信息,本人暫時不知曉。不過從網上搜索得到的方法大致是:

  • Thread.currentThread().getStackTrace()[1].getFileName():獲取當前文件名;

  • Thread.currentThread().getStackTrace()[1].getLineNumber():獲取當前行號。

  • 其中:Thread.currentThread().getStackTrace()返回的是一個數組形式的函數調用棧(棧頂在索引0處),其中第1個元素(索引為0)為最新調用的函數信息(getStackTrace()),第2個元素(索引為1)為當前函數(即調用getStackTrace()的函數)信息。

2. java記事本程序中顯示行號的程序塊

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;

public class ShowSourceCode {

public static void main(String[] args) throws FileNotFoundException, IOException{

RandomAccessFile file = new RandomAccessFile("Add.java", "r");
String tmp;

while((tmp = file.readLine()) != null){
System.out.println(tmp);
}

file.seek(0);

int lineNum = 1;
while((tmp = file.readLine()) != null){
System.out.println(lineNum + ": " + tmp);
lineNum++;
}
}

}

3. Java獲取代碼執行的代碼的所有行號

這個沒什麼意義吧,你想要知道具體運行到那些地方的話可以打斷點一步一步運行啊

4. java讀文件怎麼獲取當前行號

行號可以根據換行計算,每換一行,則行號增加,行尾都有一個

so:

  1. 讀取文件所有內容,

  2. 判斷 的個數,即可得到行數( 個數+1)

    註:計算 個數可以用 來分割字元串,得到數組,數組的length即為行號

5. java eclipse里如何看行號

在eclipse的編輯區的左側邊框上點擊滑鼠右鍵,在彈出的選項里有一行「Show Line Number」,選擇這個就行了。
如果是中文版更明顯,就叫「顯示行號」

6. 怎麼用JAVA編寫記事本 用來顯示行號

文件名換成你自己即可
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;

public class ShowSourceCode {

public static void main(String[] args) throws FileNotFoundException, IOException{

RandomAccessFile file = new RandomAccessFile("Add.java", "r");
String tmp;

while((tmp = file.readLine()) != null){
System.out.println(tmp);
}

file.seek(0);

int lineNum = 1;
while((tmp = file.readLine()) != null){
System.out.println(lineNum + ": " + tmp);
lineNum++;
}
}

}

7. java日誌是怎麼能輸出行號的

在StackTraceElement類中有個getLineNumber()方法可以獲取行號。
在Thread類中有個getStackTrace()方法可以獲取StackTraceElement數組。下標從小到大依次為方法棧從頂到底。所以說通過 Thread.currentThread().getStackTrace()[1].getLineNumber()即可得到.
當然,通過構造一個Throwable(或其子類)對象也可以得到行號。
至於為什麼有行號:位元組碼中有個LineNumberTable屬性,將位元組碼偏移地址與java行對應起來,所以可以找到。

8. java 如何列印函數名稱和行號

翻閱JDK,我們找到StackTraceElement這個類。這個類可以從Throwable取得,另外也可以從Thread類取得,通過這些我寫如下的一個列印行號的測試程序:
查看源代碼
列印幫助
01 public class LineNo {
02 public static int getLineNumber() {
03 return Thread.currentThread().getStackTrace()[2].getLineNumber();
04 }
05
06 public static String getFileName() {
07 return Thread.currentThread().getStackTrace()[2].getFileName();
08 }
09 public static void main(String args[]) {
10 System.out.println("["+getFileName()+":"+ getLineNumber()+"]"+"Hello World!");
11 }
12 }

留下一個問題,上面程序中的magic數字 2 代表什麼含義呢?

0是thread.getCurrentThread()
1是getLineNumber()
2才是調用getLineNumber的類

9. 如何在Java中獲取當前代碼行行號和文件名

如果你用的是Eclipse或MyEclipse,在代碼的左邊(就是有加號或減號,用於縮放代碼的位置)點右鍵將ShowLineNumbers打上對勾就可以看見行號了,文件名看包。

10. java為文件添加行號題

import java.io.*;
import java.util.*;

public class Test{
public static void main(String[] args){
List<String> list=new ArrayList<String>();
try {
File f=new File("d:/file/hello.txt ");
Scanner sc=new Scanner(f);
int k=0;
while(sc.hasNextLine()){
list.add(++k+" "+sc.nextLine());
}
FileWriter fw=new FileWriter(f);
for(int i=0;i<list.size();i++)
fw.write(list.get(i)+"\r\n");
fw.flush();
fw.close();
System.out.println("操作已經成功完成!");

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

熱點內容
sql2008錯誤233 發布:2025-07-03 02:28:52 瀏覽:168
創建資料庫語句mysql 發布:2025-07-03 02:14:34 瀏覽:146
python量化投資 發布:2025-07-03 02:05:11 瀏覽:804
proxy代理伺服器地址 發布:2025-07-03 01:56:52 瀏覽:910
ps選區存儲 發布:2025-07-03 01:55:21 瀏覽:842
sql2008連接數 發布:2025-07-03 01:55:20 瀏覽:246
androidstring 發布:2025-07-03 01:53:55 瀏覽:183
密碼sql注入 發布:2025-07-03 00:44:07 瀏覽:555
oa伺服器需要什麼硬體 發布:2025-07-03 00:36:05 瀏覽:512
4mol的naoh怎麼配置 發布:2025-07-03 00:19:10 瀏覽:856