I have developed a application in which user select the particular folder and it counts all the java files in that folder plus the line of code individually in those files and show at console but in a java project there are so many packages and right now I have to navigate until a particular package , I want to modify the application in such a way that when user select the particular project, he will then further navigate to only src folder and from src folder all the packages containing java files line of code will be counted.
Please advise how to achieve that..below is my piece of code:
public class abc {
/**
* @param args
* @throws FileNotFoundException
*/
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());
int totalLineCount = 0;
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;
}
} }
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"+result.size());
System.out.println("SUM OF ALL THE LINES ==>"+"\t"+ totalLineCount);
}
}
}
the problem with my code is now when I start my application an dialog box get opened in which I have to browse till the complete package folder which finally contains the java files but now I want to modify the application in such a way so that when file diaolog box get opened , user will navigate to project src folder only and from then onwards all the packages inside the src folder need to be scanned and all the files line of code,please advise..
What I was thinking was…
- Given a File Object that represents a directory (we will call it Directory):
- Get all the Files in the Directory.
- For Each File in the Directory, (we will call it thisFile) do the following:
- If thisFile is a directory, start from the beginning use thisFile as the Directory
- Else, If thisFile is a .java file, count the lines of code
- Else, ignore thisFile
This gets a whole lot easier if you separate the main into methods.
Here the code goes