统计某java文件中的代码行数、某个字符(char)、某字符串(String)出现的次数

来源:互联网 发布:收看全球电视直播软件 编辑:程序博客网 时间:2024/06/08 15:05
1、统计java文件中的代码行数。
2、统计java文件中某char字符出现的次数。
3、统计java文件中某字符串String出现的次数。
4、实现方式比较粗暴,不知道还有没其他方式,有的话,多多交流,微信:YY7920209
5、实现代码如下:
package com.util.practice;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;/** * 统计某java文件中的代码行数 * @author tiger * @Date 2017年8月13日 */public class AccountRows {public static void main(String[] args) throws IOException {String path = "E:\\xx.java";System.out.println("这段代码一共【"+accountRows(path)+"】行");}/** * 统计某java文件中的代码行数 * @param path 原文件路径 * @return 返回总的代码行数 * @throws IOException */public static int accountRows(String path) throws IOException{int count = 0;try {BufferedReader reader = new BufferedReader( new FileReader( path ) );while (reader.ready()) {reader.readLine();count++;}} catch (FileNotFoundException e) {e.printStackTrace();}return count;}}



阅读全文
1 0
原创粉丝点击