
正文
java统计代码目录 java test目录
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
java 遍历一个目录,统计目录及其子目录中各种类型文件的数目,统计完成后,打印出各种扩展名及其数目。急
那就是用流java统计代码目录的知识
package com.tx.test;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class count {
public static void main(String[] args) {
File f = new File("D:/upload");
MapString,Integer m =null;
if(f.isDirectory()) {
look(f,m);
}
}
private static MapString, Integer look(File f ,MapString,Integer m) {
File[] listFiles = f.listFiles();
for (File file : listFiles) {
if(!file.isDirectory()) {
String name = file.getName();
int index = name.indexOf(".");
String s =name.substring(index+1);
m= new HashMapString, Integer();
if(m.containsKey(s)) {
int count= m.get(s);
m.put(s, count++);
}else {
m.put(s,1);
}
System.out.println(file.getAbsolutePath());
}else {
look(file,m);
}
}
return m;
}
}
相关问答
Q1: 如何统计某目录下的java文件代码行数
可以自己写一个小程序,遍历每个文件;
如果是*.java就记录该文件的行数,依次累加。
Q2: 输入:1个目录 输出:这个目录下的java文件的代码行数总和(统计时忽视注释和空行),
//统计程序生成文件的行数
package package_1;
import java.io.*;
public class Linknum {
File tempFile1,tempFile2;
File tempFiles[] = null;
String path,temp = "";
int num = 2;
public Linknum() throws IOException{
tempFile1 = new File("");
path = tempFile1.getAbsolutePath() + "\\src\\package_1";
tempFile2 = new File(path);
tempFiles = tempFile2.listFiles();
if(tempFiles != null){
for(int i = 0;itempFiles.length;i++){
if(tempFiles[i].toString().equals(path + "\\Linknum.java")){
FileReader fr = new FileReader(tempFiles[i]);
BufferedReader br = new BufferedReader(fr);
String temp2 = br.readLine();
temp = 1 + "\t" + temp + temp2 + "\r\n" + num + "\t";
while((temp2 = br.readLine()) != null){
num++;
temp = temp + temp2 + "\r\n" + num + "\t";
}
br.close();
fr.close();
}
}
FileWriter fw = new FileWriter(new File(path,"Count.ljl"));
BufferedWriter bw = new BufferedWriter(fw);
bw.write(temp);
bw.close();
fw.close();
}
}
public Linknum(String s)throws IOException{
tempFile1 = new File("");
path = tempFile1.getAbsolutePath() + "\\src\\package_1";
tempFile2 = new File(path);
tempFiles = tempFile2.listFiles();
if(tempFiles != null){
for(int i = 0;itempFiles.length;i++){
if(tempFiles[i].toString().equals(path + "\\Linknum.java")){
FileReader fr = new FileReader(tempFiles[i]);
BufferedReader br = new BufferedReader(fr);
String temp2 = br.readLine();
temp = 1 + ". " + temp + temp2 + "\r\n" + num + ". ";
while((temp2 = br.readLine()) != null){
num++;
temp = temp + temp2 + "\r\n" + num + ". ";
}
br.close();
fr.close();
}
}
FileWriter fw = new FileWriter(new File(s,"Count.ljl"));
BufferedWriter bw = new BufferedWriter(fw);
bw.write(temp);
bw.close();
fw.close();
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入保存文件的路径(或输入空格默认):");
String str = br.readLine();
br.close();
if(str.equals(" "))
new Linknum();
else
new Linknum(str);
System.out.println("行数统计完毕,请查看。");
}
}
差不多,自己改改吧,我这个不能忽略注释和空行。
Q3: 解压 JDK 中的 src.zip 文件,在 FileUtil 类中统计 java 目录(java包)中:
代码如下:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
final class FileUtil {
/**
* 解压 zip 文件到指定路径
* @param zipFilePath
* @param unzipDirPath
* @return
* @throws IOException
*/
public static void unzip(String zipFilePath, String unzipDirPath) throws IOException {
unzip(new File(zipFilePath), unzipDirPath);
}
/**
* 解压 zip 文件到指定路径
* @param zipFile zip 文件
* @param unzipDirPath 解压路径
* @throws IOException
*/
public static void unzip(File zipFile, String unzipDirPath) throws IOException {
File unzipDir = new File(unzipDirPath);
// 如果解压目录不存在,则创建解压目录
if (!unzipDir.exists()) {
unzipDir.mkdirs();
}
try (ZipFile zip = new ZipFile(zipFile)) {
Enumeration? extends ZipEntry entries = zip.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String file = entry.getName();
int index = file.lastIndexOf("/");
String entryDirPath = unzipDirPath;
if (index != -1) {
entryDirPath = unzipDirPath + File.separator + file.substring(0, index);
}
File entryDir = new File(entryDirPath);
if (!entryDir.exists()) {
entryDir.mkdirs();
}
String entryFilePath = unzipDirPath + File.separator + file;
try ( BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(entryFilePath));
BufferedInputStream bis = new BufferedInputStream(zip.getInputStream(entry)) ) {
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bis.read(buffer)) != -1) {
bos.write(buffer, 0, len);
}
}
}
}
}
/**
* 统计指定目录及其子目录下的文件或文件夹数量
* @param dirPath 需要统计的目录路径
* @param filter 过滤器
* @return 统计数量
* @throws FileNotFoundException 如果指定的目录不存在,则抛出此异常
*/
public static int count(String dirPath, FileFilter filter) throws FileNotFoundException {
return count(new File(dirPath), filter);
}
/**
* 统计指定目录及其子目录下的文件或文件夹数量
* @param dir 需要统计的目录
* @param filter 过滤器
* @return 统计数量
* @throws FileNotFoundException 如果指定的目录不存在,则抛出此异常
*/
public static int count(File dir, FileFilter filter) throws FileNotFoundException {
int count = 0;
if (!dir.exists()) {
throw new FileNotFoundException(dir.getPath());
}
for (File file : dir.listFiles()) {
if (filter.accept(file)) {
count++;
}
if (file.isDirectory()) {
count += count(file.getAbsolutePath(), filter);
}
}
return count;
}
/**
* 统计指定目录及其子目录下的所有文件内容行数总和
* @param dirPath 需要进行统计的目录路径
* @param filter 过滤器
* @return 行数总和
* @throws IOException
*/
public static int countFileLines(String dirPath, FilenameFilter filter) throws IOException {
return countFileLines(new File(dirPath), filter);
}
/**
* 统计指定目录及其子目录下的所有文件内容行数总和
* @param dir 需要进行统计的目录
* @param filter 过滤器
* @return 行数总和
* @throws IOException
*/
public static int countFileLines(File dir, FilenameFilter filter) throws IOException {
if (!dir.exists()) {
throw new FileNotFoundException(dir.getAbsolutePath());
}
int numberOfLines = 0;
for (File file : dir.listFiles()) {
if (file.isFile() filter.accept(dir, file.getName())) {
numberOfLines += countFileLines(file);
} else if (file.isDirectory()) {
numberOfLines += countFileLines(file, filter);
}
}
return numberOfLines;
}
/**
* 统计指定文件内容行数
* @param file 需要进行统计的文件
* @return 文件内容行数
* @throws FileNotFoundException 如果指定文件不存在,则抛出此异常
* @throws IOException
*/
public static int countFileLines(File file) throws FileNotFoundException, IOException {
if (file == null) {
throw new NullPointerException("file");
}
if (!file.isFile()) {
return 0;
}
int numberOfLines = 0;
try ( InputStreamReader streamReader = new InputStreamReader(new FileInputStream(file));
BufferedReader bufferedReader = new BufferedReader(streamReader)) {
while(bufferedReader.readLine() != null) {
numberOfLines++;
}
}
return numberOfLines;
}
}
public class App {
public static void main(String[] args) throws IOException {
// 获取 Java 安装目录,如果没有则需手动设置目录
String javaHome = System.getenv("JAVA_HOME");
// src.zip 文件
File srcZip = new File(javaHome + File.separator + "src.zip");
// 解压目录路径
String unzipDirPath = "d:\\temp\\java";
System.out.println("正在解压 src.zip ...");
// 解压 src.zip 到 d:\temp\java 文件夹
FileUtil.unzip(srcZip.getAbsolutePath(), unzipDirPath);
System.out.println("正在统计文件夹数量 ...");
// 统计文件夹数量
int dirCount = FileUtil.count(unzipDirPath, f - f.isDirectory());
System.out.println("正在统计Java文件数量 ...");
// 统计Java文件数量
int fileCount = FileUtil.count(unzipDirPath, f - f.isFile() f.getName().endsWith(".java"));
System.out.println("正在统计所有Java文件的总行数 ...");
// 统计 .java 文件内容行数
int numberOfFileLines = FileUtil.countFileLines(unzipDirPath, (dir, name) - name.endsWith(".java"));
System.out.println("文件夹数量:" + dirCount);
System.out.println("文件数量:" + fileCount);
System.out.println("所有Java文件的总行数:" + numberOfFileLines);
}
}
关于java统计代码目录和java test目录的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。







