I have developed a Java program that will count the number of files in the folder. There may be Java files or text files, of which it will count the number of lines of code. The idea is to print the file name to the console, followed by the line count. This part is done, shown below:
public class FileCountLine {
public static void main(String[] args) throws FileNotFoundException {
Map<String, Integer> result = new HashMap<String, Integer>();
File directory = new File("C:/Test/");
File[] files = directory.listFiles();
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);
}
}}
for( Map.Entry<String, Integer> entry:result.entrySet()){
System.out.println(entry.getKey()+" ==> "+entry.getValue());
}
}}
Right now I have hard coded the location of the directory like this:
File directory = new File("C:/Test/");
I want this to be more interactive, and prompt the user to enter the location into console. After the user presses Enter, it will do the rest of the functionality as is.
You can use System.out.println() to output a prompt to the console screen. Then, to get their path, use the Scanner class in java.util eg,
That string, input, can be your folder path but you probably want to do some error checking on the input for the correct format, good characters, etc