我开发了一个应用程序,用户在其中选择特定文件夹,它计算该文件夹中的所有 java 文件以及这些文件中单独的代码行并显示在控制台中,但是在 java 项目中,有很多包,现在我有导航到一个特定的包,我想修改应用程序,当用户选择特定项目时,他将进一步导航到仅 src 文件夹,并且从 src 文件夹中所有包含 java 文件行的包都将被计算在内.
public class abc {
/**
* @param args
* @throws FileNotFoundException
*/
private static int totalLineCount = 0;
private static int totalFileScannedCount = 0;
public static void main(String[] args) throws FileNotFoundException {
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("C:" + File.separator));
chooser.setDialogTitle("FILES ALONG WITH LINE NUMBERS");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
Map<String, Integer> result = new HashMap<String, Integer>();
File directory = new File(chooser.getSelectedFile().getAbsolutePath());
List<File> files = getFileListing(directory);
//print out all file names, in the the order of File.compareTo()
for (File file : files) {
System.out.println("Directory: "+file);
result = getFileLineCount(file);
totalFileScannedCount += result.size();
}
System.out.println("*****************************************");
System.out.println("FILE NAME FOLLOWED BY LOC");
System.out.println("*****************************************");
for (Map.Entry<String, Integer> entry : result.entrySet()) {
System.out.println(entry.getKey() + " ==> " + entry.getValue());
}
System.out.println("*****************************************");
System.out.println("SUM OF FILES SCANNED ==>" + "\t" + totalFileScannedCount);
System.out.println("SUM OF ALL THE LINES ==>" + "\t" + totalLineCount);
}
}
public static Map<String, Integer> getFileLineCount(File directory) throws FileNotFoundException {
Map<String, Integer> result = new HashMap<String, Integer>();
File[] files = directory.listFiles(new FilenameFilter() {
@Override
public boolean accept(File directory, String name) {
if (name.endsWith(".java")) {
return true;
} else {
return false;
}
}
});
for (File file : files) {
if (file.isFile()) {
Scanner scanner = new Scanner(new FileReader(file));
int lineCount = 0;
try {
for (lineCount = 0; scanner.nextLine() != null; lineCount++);
} catch (NoSuchElementException e) {
result.put(file.getName(), lineCount);
totalLineCount += lineCount;
}
}
}
return result;
}
/**
* Recursively walk a directory tree and return a List of all
* Files found; the List is sorted using File.compareTo().
*
* @param aStartingDir is a valid directory, which can be read.
*/
static public List<File> getFileListing(
File aStartingDir) throws FileNotFoundException {
validateDirectory(aStartingDir);
List<File> result = getFileListingNoSort(aStartingDir);
Collections.sort(result);
return result;
}
// PRIVATE //
static private List<File> getFileListingNoSort(
File aStartingDir) throws FileNotFoundException {
List<File> result = new ArrayList<File>();
File[] filesAndDirs = aStartingDir.listFiles();
List<File> filesDirs = Arrays.asList(filesAndDirs);
for (File file : filesDirs) {
if(file.isDirectory()) {
result.add(file);
}
if (!file.isFile()) {
//must be a directory
//recursive call!
List<File> deeperList = getFileListingNoSort(file);
result.addAll(deeperList);
}
}
return result;
}
/**
* Directory is valid if it exists, does not represent a file, and can be read.
*/
static private void validateDirectory(
File aDirectory) throws FileNotFoundException {
if (aDirectory == null) {
throw new IllegalArgumentException("Directory should not be null.");
}
if (!aDirectory.exists()) {
throw new FileNotFoundException("Directory does not exist: " + aDirectory);
}
if (!aDirectory.isDirectory()) {
throw new IllegalArgumentException("Is not a directory: " + aDirectory);
}
if (!aDirectory.canRead()) {
throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
}
}
}
现在的问题是这种方法的问题是,如果在名为 TESTRESULT 的 java 项目中有 3 个包并且每个包有 2 个文件,那么在控制台的结果中它显示文件名后跟 loc 仅最新包只让说一个名为 abc、def 和 tyu 的 java projest 3 包
com.abc package having files --->abc.java
com.def package having files --->abc.java
com.tyu package having files --->FileBrowse.java , FileCountLine.java
the outcome shown in console is...
Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\.settings
Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\bin
Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src
Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\com
Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\com\abc
Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\com\def
Directory: C:\Users\vaio\Desktop\Demo\TESTRESULT\src\tyu
*****************************************
FILE NAME FOLLOWED BY LOC
*****************************************
FileBrowse.java ==> 95
FileCountLine.java ==> 53
*****************************************
SUM OF FILES SCANNED ==> 4
SUM OF ALL THE LINES ==> 296
这并不完美。请告知它将如何显示所有文件的 LOC 后跟的文件名
我的想法是在读取文件和行数的方法中,您为每个目录创建一个 NEW Map 并将其重新分配给相同的结果。由于您在重新分配地图之前不显示数据,因此以前的地图不再可访问,并且当您到达显示时,您只有最后一个目录的地图。相反,您应该维护一个 Map 并将每个目录中的新值插入到同一个 Map 中。
public static void main(String[] args) throws FileNotFoundException {
//...
for (File file : files) {
System.out.println("Directory: "+file);
result = getFileLineCount(file);
在那里你得到一个 Map 回来getFileLineCount()
,并将其分配给结果。这会丢弃先前创建的结果 Map,并且您会丢失已经存在的所有内容。你有几个选择:
- 将结果 Map 传递给方法,以便您可以将结果添加到一个 Map,而不是为方法
getFileLineCount()
内的每个文件夹创建一个新 MapgetFileLineCount()
- 从中获取结果
getLineCount()
并将它们复制到结果映射中,而不是替换结果映射 - 创建另一个集合,如 Map 之类的:(
Map<String, Map<String, Integer>>
它将getFileLineCount()
方法返回的结果映射到这些结果所属的目录的名称),或者List<Map<String, Integer>>
(这将是返回的结果的简单列表,getFileLineCount()
没有到父级的任何映射目录)。
请告知如何重构我的代码,在此先感谢