File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Regarding absolute path Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Regarding absolute path" Watch "Regarding absolute path" New topic
Author

Regarding absolute path

naresh saxena
Greenhorn

Joined: Jun 27, 2012
Posts: 22
Hi Folks,
I have one query is that I have developed a program that ask from user to enter the drive( c: d and the folder name and based on these parameters it calculates the lines of code in that folder of all the files inside that folder but now the problem is lets say one user has kept the folder in C:\Test\ and other user has kept the folder in C:\abc\Test\ so nesting could happen , so to solve this issue ,I was thinking of the logic that to take full absolute path from the user itself , and based on that absolute path my current program will work , please advise how to achieve this, my present logic is



Please advise how to add the logic of taking the full absolute path from the user and then working on that absolute path
One more thing is that I was thinking is that from the user will enter the complete path lets say c:/abc/def/Test and that we will accept it as string and based on that we will continue with our existing logic , please advise
E Armitage
Ranch Hand

Joined: Mar 17, 2012
Posts: 220
Yes, the location of the files is not something you can control or influence in your application so it makes sense to make the user provide all of it.
Some people could be running on unix environments as well.
naresh saxena
Greenhorn

Joined: Jun 27, 2012
Posts: 22
E Armitage wrote:Yes, the location of the files is not something you can control or influence in your application so it makes sense to make the user provide all of it.
Some people could be running on unix environments as well.


I am talking of windows requirement only ,One more thing is that I was thinking is that from the user will enter the complete path lets say c:/abc/def/Test and that we will accept it as string and based on that we will continue with our existing logic , please advise
E Armitage
Ranch Hand

Joined: Mar 17, 2012
Posts: 220
naresh saxena wrote:
E Armitage wrote:Yes, the location of the files is not something you can control or influence in your application so it makes sense to make the user provide all of it.
Some people could be running on unix environments as well.


One more thing is that I was thinking is that from the user will enter the complete path lets say c:/abc/def/Test and that we will accept it as string and based on that we will continue with our existing logic , please advise

Sounds viable. You have to worry about the validity of the entered path though because some users will enter an invalid path.
Another option is to add gui capabilities to the program and use a JFileChooser instead.
naresh saxena
Greenhorn

Joined: Jun 27, 2012
Posts: 22
E Armitage wrote:
Sounds viable. You have to worry about the validity of the entered path though because some users will enter an invalid path.
Another option is to add gui capabilities to the program and use a JFileChooser instead.


Thanks could you please show me how to use jfilechooser as when I am browsing to the particular folder , I want to select the folder only and all the files in that folder to be get selected and the logic of displaying the file name along with Counting lines of code would be the same one as I have shown, Please advise
E Armitage
Ranch Hand

Joined: Mar 17, 2012
Posts: 220
You tell the chooser to select directories only using

See more details here:http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

naresh saxena
Greenhorn

Joined: Jun 27, 2012
Posts: 22
E Armitage wrote:You tell the chooser to select directories only using

See more details here:http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html



Thanks, I have come up with this below piece of code .. first please advise where the changes need to be done so that user click on directory test and it will accept the directory



This is my UI but i want to integrate my first piece of code to count the number of lines in all the file , please advise how to do that
E Armitage
Ranch Hand

Joined: Mar 17, 2012
Posts: 220
You can use a structure like:


Your actionPerformed logic should ideally run in its own thread as well if it is to do with IO over an indefinite number and size of files.
naresh saxena
Greenhorn

Joined: Jun 27, 2012
Posts: 22
Hi Folks,
I have come up with the solution but it is selecting only file , my code is..

but it is selecting only single file
E Armitage
Ranch Hand

Joined: Mar 17, 2012
Posts: 220
Use chooser.getSelectedFile(); instead of chooser.getCurrentDirectory();
naresh saxena
Greenhorn

Joined: Jun 27, 2012
Posts: 22
E Armitage wrote:Use chooser.getSelectedFile(); instead of chooser.getCurrentDirectory();


come up with solution that is working..Here below is the piece of code..

E Armitage
Ranch Hand

Joined: Mar 17, 2012
Posts: 220
You don't need to do

choosers.getSelectedFile() already returns a File so just assign that rather than creating another file object

naresh saxena
Greenhorn

Joined: Jun 27, 2012
Posts: 22
E Armitage wrote:You don't need to do

choosers.getSelectedFile() already returns a File so just assign that rather than creating another file object



Thanks Armitage , could you also please advise let say I want initially upon executing the application a main window should come with two button ENTER and EXIT , upon clicking the EXIT the application should end's up and upon clicking on ENTER the my application will open the dialog to select the folder as it is doing presently. what the main objective an main window with two buttons should com up first , please advise how to achiev that and integrate with the present application
E Armitage
Ranch Hand

Joined: Mar 17, 2012
Posts: 220
See the example structure code (including comments) I posted above.
But what you really need to learn is how to use a layout manager for your swing components. In this case you could use a CardLayout. The excellent tutorial for it is here: http://docs.oracle.com/javase/tutorial/uiswing/layout/card.html
naresh saxena
Greenhorn

Joined: Jun 27, 2012
Posts: 22
E Armitage wrote:


Thanks a lot it works , could you also please advise when I execute this application , it is by default when dialog box get opened it is in some other folder location by default , I want that upon execution it is by default in c: location , further user can navigate ,please advise how to achieve this, Thanks in advance.
E Armitage
Ranch Hand

Joined: Mar 17, 2012
Posts: 220
See the API specs for the JFileChooser constructor that takes a String. Careful that you don't resurrect the problem of coding to a specific platform. Remember that some users won't have a C:\\
You could try fiddling with things like File.listRoots to be sure but I'd think that most users would prefer the default to point to their own default documents folder than to the system's root folder.
naresh saxena
Greenhorn

Joined: Jun 27, 2012
Posts: 22
E Armitage wrote:See the API specs for the JFileChooser constructor that takes a String. Careful that you don't resurrect the problem of coding to a specific platform. Remember that some users won't have a C:\\
You could try fiddling with things like File.listRoots to be sure but I'd think that most users would prefer the default to point to their own default documents folder than to the system's root folder.


Thanks Achieve that through ...
 
I agree. Here's the link: http://jrebel.com/download
 
subject: Regarding absolute path
 
Similar Threads
Exception error? Please help, frustrating
Problems w/ a HashMap
Canonical vs Absolute
Reading in a file