Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

access .txt files under the particlar parent directory  RSS feed

 
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 ?
 
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
 
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-
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!