• Post Reply Bookmark Topic Watch Topic
  • New Topic

access .txt files under the particlar parent directory  RSS feed

 
ashok ks
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If i give parent directory name dymamically, particular java program should aceess its .txt extension files under its subdirectories ?
 
Donny Nadolny
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get a listing of all files in the current directory see this thread, actually its short enough that I might as well post the code again.

Now, rather than printing out the filename, you can use the string function (stringname).endsWith(".txt") to see if it has the .txt extention. That will give you all .txt files in the current directory. To do sub directories, you will need to loop through and (recursively) check each subsequent directory for txt files as well as more directorys.
If you need help with recusrion, check out a google search here.
If you need more help, just post.
 
ashok ks
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my recursive of sub-directories does not work. If u have sample code just post it
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Poorly factored code but it should work. Note that you can also attach a file filter to the listFiles method call to return only *.txt and directories; this way was just as easy.

[ February 11, 2004: Message edited by: Jeff Langr ]
 
ashok ks
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jeff Langr.
System.getProperties().getProperty("user.dir"); -> it starts from the current working directory.
If i want to give any directory path (like D:/hai/bye ), then how can i dynamically mention my directory path ?
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pass it in via the command line. Use the args parameter to main to extract it:

-j-
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!