• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regarding reading all the packages and all the files inside those packages from src directory itself  RSS feed

 
naresh saxena
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,
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 cotaining java files line of code will be counted.

Please advise how to achieve that..below is my piece of code:



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
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Strategy would be:

- 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. For example, if you had method like:



Then you could just call this same method each time you come in contact with a directory (the one the user selects, and all the sub-directories).
 
naresh saxena
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:The Strategy would be:

- 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. For example, if you had method like:



Then you could just call this same method each time you come in contact with a directory (the one the user selects, and all the sub-directories).


Hi ,
Have come up with this approach shown below..



but the issue with this approach is that if in java project named TESTRESULT there are 3 packages and each package having 2 files each then in the result on console it shows file name followed by loc of only the latest package only lets say in a java projest 3 packages named abc, def and tyu

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


which is not perfect. please advise HOW IT WILL SHOW FILENAME FOLLOWED BY LOC for all the files

 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the method that reads the files and the line count, you create a NEW Map for each directory and re-assign it to the same result. Since you do not display the data before you re-assign the Map, the previous Map is no longer accessible and by the time you get to the display you only have a Map for the last directory. Instead, you should maintain a single Map and insert new values from each directory into the same Map.
 
naresh saxena
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:In the method that reads the files and the line count, you create a NEW Map for each directory and re-assign it to the same result. Since you do not display the data before you re-assign the Map, the previous Map is no longer accessible and by the time you get to the display you only have a Map for the last directory. Instead, you should maintain a single Map and insert new values from each directory into the same Map.


Thanks a lot , steve can you do a favor to me could you please explain and refactor above my code with your changes that will be a great help , I AM STUCK UP , your valuable contribution would be a great help..!!Thanks in advance
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, I won't write the code for you. I will highlight the part that needs to change:

Right there you get a Map back from getFileLineCount(), and assign it to result. This discards the previously created result Map, and you lose everything that was already there. You have a few options:
1) Pass the result Map into the getFileLineCount() method so you can add results to the one Map, rather than creating a new Map for each folder inside the getFileLineCount() method
2) Take the results from getLineCount() and copy them into the result Map rather than replacing the result Map
3) Create another collection, like a Map like: Map<String, Map<String, Integer>> (which would map the results returned by the getFileLineCount() method to the name of the directory those results pertain to), or List<Map<String, Integer>> (which would be a simple list of the results returned by getFileLineCount() without any mapping to the parent directory).

 
naresh saxena
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Steve Luke wrote:No, I won't write the code for you. I will highlight the part that needs to change:

Right there you get a Map back from getFileLineCount(), and assign it to result. This discards the previously created result Map, and you lose everything that was already there. You have a few options:
1) Pass the result Map into the getFileLineCount() method so you can add results to the one Map, rather than creating a new Map for each folder inside the getFileLineCount() method
2) Take the results from getLineCount() and copy them into the result Map rather than replacing the result Map
3) Create another collection, like a Map like: Map<String, Map<String, Integer>> (which would map the results returned by the getFileLineCount() method to the name of the directory those results pertain to), or List<Map<String, Integer>> (which would be a simple list of the results returned by getFileLineCount() without any mapping to the parent directory).




Achieve that man..Thanks for the suggestion

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!