基本的模式已經給你了,你可以自己變通修改下,要有這方面的能力
創新互聯為您提適合企業的網站設計?讓您的網站在搜索引擎具有高度排名,讓您的網站具備超強的網絡競爭力!結合企業自身,進行網站設計及把握,最后結合企業文化和具體宗旨等,才能創作出一份性化解決方案。從網站策劃到成都網站設計、網站建設, 我們的網頁設計師為您提供的解決方案。
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Admin {
public static void main(String... args) {
try {
Scanner in = new Scanner(new File("D:/gao.java"));
int nulSum = 0;
int zhushiSum = 0;
while (in.hasNext()) {
String str = in.nextLine().trim();
if ("".equals(str)) {
nulSum++;
}
if (str.startsWith("http://")) {
zhushiSum++;
}
}
System.out.println("空行:" + nulSum);
System.out.println("注釋:" + zhushiSum);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
import java.io.*;public class Check {
public static void main (String[] args) throws IOException{
check("D:/MyEclipse/java/src/my/Check.java");//這里的字符串是你要統計的文件的路徑,你自己填寫
} public static void check (String s) throws IOException{
int all = 0, empty = 0, describe = -1, i = 0;
String str = null;
File f = new File(s);
BufferedReader br = new BufferedReader (new FileReader(f));
str = br.readLine();
while(str != null){
all++;
if(str.trim().equals("")) empty++;
if(str.contains("http://")) describe++;
if(str.contains("/*")){
while(!str.contains("*/")){
i++;
all++;
describe++;
str = br.readLine();
}
}
str = br.readLine();
}
System.out.println("文件物理總行數為:" + all);//;;klj
System.out.println("文件中空行數為:" + empty);//hkk
System.out.println("文件注釋行數為:" + describe);
System.out.println("文件非注釋行數為:" + (all - i));
/*asdfdsff
* sdasadfsf//fg
* asdfsdf//dsfg
* asdf
*/
}
}以上是代碼,我在我的機子上實現了,希望能幫到你!我也是JAVA菜鳥,希望有高手能更好地解答
步驟如下:
1、打開File Search對話框。
2、選中正則表達式,在搜索文本框輸入\n 。
3、文件名稱輸入 *.java。
4、在范圍里選中Enclosing projects。
經過上面方式,就可以統計出整個項目的代碼行數。
方法一:
如果想要通過java代碼的方式來計算.java文件的行數,可以通過IO來讀取,
BufferedReader的方法readLine()來按行讀取,每讀取一行,行數+1
方法二:
如果要查看.java文件的代碼行數,
可以使用現成的IDE工具,比如ECLIPSE...
每一行的行號都有表示出來
package com.syl.demo.test;
import java.io.*;
/**
* java代碼行數統計工具類
* Created by 孫義朗 on 2017/11/17 0017.
*/
public class CountCodeLineUtil {
private static int normalLines = 0; //有效程序行數
private static int whiteLines = 0; //空白行數
private static int commentLines = 0; //注釋行數
public static void countCodeLine(File file) {
System.out.println("代碼行數統計:" + file.getAbsolutePath());
if (file.exists()) {
try {
scanFile(file);
} catch (IOException e) {
e.printStackTrace();
}
} else {
System.out.println("文件不存在!");
System.exit(0);
}
System.out.println(file.getAbsolutePath() + " ,java文件統計:" +
"總有效代碼行數: " + normalLines +
" ,總空白行數:" + whiteLines +
" ,總注釋行數:" + commentLines +
" ,總行數:" + (normalLines + whiteLines + commentLines));
}
private static void scanFile(File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i files.length; i++) {
scanFile(files[i]);
}
}
if (file.isFile()) {
if (file.getName().endsWith(".java")) {
count(file);
}
}
}
private static void count(File file) {
BufferedReader br = null;
// 判斷此行是否為注釋行
boolean comment = false;
int temp_whiteLines = 0;
int temp_commentLines = 0;
int temp_normalLines = 0;
try {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
line = line.trim();
if (line.matches("^[//s[^//n]]*$")) {
// 空行
whiteLines++;
temp_whiteLines++;
} else if (line.startsWith("/*") !line.endsWith("*/")) {
// 判斷此行為"/*"開頭的注釋行
commentLines++;
comment = true;
} else if (comment == true !line.endsWith("*/")) {
// 為多行注釋中的一行(不是開頭和結尾)
commentLines++;
temp_commentLines++;
} else if (comment == true line.endsWith("*/")) {
// 為多行注釋的結束行
commentLines++;
temp_commentLines++;
comment = false;
} else if (line.startsWith("http://")) {
// 單行注釋行
commentLines++;
temp_commentLines++;
} else {
// 正常代碼行
normalLines++;
temp_normalLines++;
}
}
System.out.println(file.getName() +
" ,有效行數" + temp_normalLines +
" ,空白行數" + temp_whiteLines +
" ,注釋行數" + temp_commentLines +
" ,總行數" + (temp_normalLines + temp_whiteLines + temp_commentLines));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
br = null;
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
//測試
public static void main(String[] args) {
File file = new File("F:\\myweb");
countCodeLine(file);
}
}
問題1:在編輯區最左邊地方右鍵,選擇“Show Line Numbers”就行了。
問題2:快捷鍵(ctrl+f)
問題3:在工程名上右鍵,選擇“Refactor-Rename”。
希望對你有幫助!
網站標題:java顯示代碼行數 java如何顯示行數
URL鏈接:http://vcdvsql.cn/article44/hhpcee.html
成都網站建設公司_創新互聯,為您提供云服務器、商城網站、手機網站建設、虛擬主機、靜態網站、網站維護
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯