java文件行數
1. 如何統計某目錄下的java文件代碼行數
可以自己寫一個小程序,遍歷每個文件;
如果是*.java就記錄該文件的行數,依次累加。
2. 一個java類標准代碼行數范圍大概是多少
以1000行為准,超過千行就要考慮類拆分了。
類的代碼行數沒有特定的行數限制規范。根據實際情況決定。
對於經常使用的java類,代碼行數應該盡可能的少,這樣能減少java類的載入時間,減少內存頻繁佔用和回收。如果類過大,java類載入會耗時並且佔用內存大。容易引起內存回收。
3. java怎麼讀取txt文件的行數
importjava.io.File;
importjava.io.RandomAccessFile;
/**
*讀取文檔的第二行內容
*
*@author33062017年3月21日
*@see
*@since1.0
*/
publicclassCountLine{
/*
*讀取文件絕對路徑
*/
privatestaticStringfilePath="d:/cms.sql";
publicstaticvoidmain(String[]args){
longline=readLine(filePath);
System.out.println(line);
}
/**
*讀取文件行數
*
*@parampath
*文件路徑
*@returnlong
*/
publicstaticlongreadLine(Stringpath){
longindex=0;
try{
RandomAccessFilefile=newRandomAccessFile(newFile(path),"r");
while(null!=file.readLine()){
index++;
}
file.close();
}catch(Exceptione){
e.printStackTrace();
}
returnindex;
}
}
4. java中怎麼獲得一個文本文件的行數
涉及到java中讀寫文件的IO操作。
獲取一個文本文件的行數較為方便的方法,是通過BufferedReader類的readLine()方法,間接的統計行數。
源代碼:
public
static
int
getTextLines()
throws
IOException
{
String
path
=
"c:\\job.txt"
;//
定義文件路徑
FileReader
fr
=
new
FileReader(path);
//這里定義一個字元流的輸入流的節點流,用於讀取文件(一個字元一個字元的讀取)
BufferedReader
br
=
new
BufferedReader(fr);
//
在定義好的流基礎上套接一個處理流,用於更加效率的讀取文件(一行一行的讀取)
int
x
=
0;
//
用於統計行數,從0開始
while(br.readLine()
!=
null)
{
//
readLine()方法是按行讀的,返回值是這行的內容
x++;
//
每讀一行,則變數x累加1
}
return
x;
//返回總的行數
}
5. java IO流讀文件能不能得到行數
理論上說是不能直接得到的,因為最原始的IO流是位元組流也就是說一次只能讀到一個位元組的數據,這裡面根本就沒有行數之說;此外,即使使用緩沖流最多也就是每次讀進一行已,如果想要得到文件的行數就得等文件讀完才知道。
6. java 項目 代碼行數多少 算大項目
java項目的大小衡量標准:
微型:只是一個人,甚至是半日工作在幾天內完成的軟體;
小型:一個人半年內完成的2000行以內的程序;
中型:5個人在1年多的時間內完成的5000-50000行的程序;
大型:5-10人在兩年內完成的50000-100000行的程序;
甚大型:100-1000人參加用4-5年完成的具有100,0000行的軟體項目;
極大型:2000-5000人參加,10年內完成的1000萬行以內的程序;
以上摘自:《軟體工程概論》鄭人傑、殷人民編
這樣的觀點是以代碼行作為計量標準的,認為代碼行多的自然項目也就大了。
7. 一個java文件裡面最多寫多少行代碼
1,java規范中一般不建議一個java類中超過500行
2,一行的長度不超過200個
3,按照規定格式將代碼格式化
8. Java 有什麼好的代碼行數,注釋行數統計工具
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("//")) {
// 單行注釋行
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);
}
}
9. 如何統計輸入內容的行數,java
如果是讀入文件統計行數,用readline,每讀一行,計數加1
如果是在控制台輸入,每行做為一個字元串,用endwith判斷是否以
換行符
結尾,true加1
10. 用java編寫 統計文件中的字元數 單詞數和行數
在C盤新建文件1.txt,輸入任意字元,如下圖: