Java 托利党,可以阅读。 */ 静态公共列表getFileListing(最终文件aStartingDir)引发FileNotFoundException{ validateDirectory(aStartingDir); 列表结果=getFileListing
托利党,可以阅读。 */ 静态公共列表getFileListing(最终文件aStartingDir)引发FileNotFoundException{ validateDirectory(aStartingDir); 列表结果=getFileListingNoSort(aStartingDir); 集合。排序(结果); 返回结果; } //私人的// 静态私有列表getFileListingNoSort(最终文件aStartingDir)引发FileNotFoundException{ 列表结果=新建ArrayList(); File[]filesAndDirs=aStartingDir.listFiles(); List filesDirs=Arrays.asList(filesAndDirs); for(文件:filesDirs){ if(file.isDirectory()){ 结果.添加(文件); } 如果(!file.isFile()){ //必须是一个目录 //递归调用! List deeperList=getFileListingNoSort(文件); 结果:addAll(deeperList); } } 返回结果; } /** *目录是有效的,如果它存在,不代表文件,并且可以 *阅读。 */ 静态私有void validateDirectory(最终文件addirectory)引发FileNotFoundException{ if(aDirectory==null){ 抛出新的IllegalArgumentException(“目录不应为null”); } 如果(!aDirectory.exists()){ 抛出新的FileNotFoundException(“目录不存在:“+A目录”); } 如果(!aDirectory.isDirectory()){ 抛出新的IllegalArgumentException(“不是目录:“+a目录”); } 如果(!aDirectory.canRead()){ 抛出新的IllegalArgumentException(“无法读取目录:“+A目录”); } } }Java 托利党,可以阅读。 */ 静态公共列表getFileListing(最终文件aStartingDir)引发FileNotFoundException{ validateDirectory(aStartingDir); 列表结果=getFileListing,java,collections,map,Java,Collections,Map,托利党,可以阅读。 */ 静态公共列表getFileListing(最终文件aStartingDir)引发FileNotFoundException{ validateDirectory(aStartingDir); 列表结果=getFileListingNoSort(aStartingDir); 集合。排序(结果); 返回结果; } //私人的// 静态私有列表getFileListingNoSort(最终文件aStartingDir)引发FileNotFoundException{ 列表结果
1+表示答案,10+表示通读整个问题。如答案中所述,代码发生了更改,更改的内容很少;)把它原样加回去,是我的错;)回答得好。我想补充一点,一般来说,我更喜欢使用File对象(如果有)而不是文件的字符串名。是的,问题是在扫描文件时应该增加它。您执行的类似操作:
totalFileScannedCount+=result.size()
应该移到这里:如果(file.isFile()){
Scanner Scanner=new Scanner(new FileReader(file));那么,增量将发生在考虑文件的情况下。
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);
}
}
}
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
public static void main(String[] args) throws FileNotFoundException {
//...
for (File file : files) {
System.out.println("Directory: "+file);
result = getFileLineCount(file);
result.put(file, lineCount);
result.put(file.getName(), lineCount);
for (Map.Entry<File, Integer> entry : result.entrySet()) {
System.out.println(entry.getKey().getName() + " ==> " + entry.getValue());
}
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Scanner;
import javax.swing.JFileChooser;
public class abc {
/**
* @param args
* @throws FileNotFoundException
*/
private static int totalLineCount = 0;
private static int totalFileScannedCount = 0;
public static void main(final 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<File, Integer> result = new HashMap<File, 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);
getFileLineCount(result, file);
}
System.out.println("*****************************************");
System.out.println("FILE NAME FOLLOWED BY LOC");
System.out.println("*****************************************");
for (Map.Entry<File, Integer> entry : result.entrySet()) {
System.out.println(entry.getKey().getAbsolutePath() + " ==> " + 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 void getFileLineCount(final Map<File, Integer> result, final File directory)
throws FileNotFoundException {
File[] files = directory.listFiles(new FilenameFilter() {
public boolean accept(final File directory, final String name) {
if (name.endsWith(".java")) {
return true;
} else {
return false;
}
}
});
for (File file : files) {
if (file.isFile()) {
totalFileScannedCount++;
Scanner scanner = new Scanner(new FileReader(file));
int lineCount = 0;
try {
for (lineCount = 0; scanner.nextLine() != null; lineCount++) {
;
}
} catch (NoSuchElementException e) {
result.put(file, lineCount);
totalLineCount += lineCount;
}
}
}
}
/**
* 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(final File aStartingDir) throws FileNotFoundException {
validateDirectory(aStartingDir);
List<File> result = getFileListingNoSort(aStartingDir);
Collections.sort(result);
return result;
}
// PRIVATE //
static private List<File> getFileListingNoSort(final 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(final 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);
}
}
}