• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

List particular file types from dynamic folders in java

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have folder structure like rootfolder/dynamic folders/test

test folder has XML files and text files.
I need to to list only XML files.
Need Something like below
rootfolder/**/test*.xml
 
lowercase baba
Posts: 13086
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried?  What did it do different than what you expected?
 
Saloon Keeper
Posts: 26541
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome the the Ranch, Velan!

I'm not sure that you want  to "list" files so much as enumerate them or collect their names/paths. To "list" files in Linux, a command line would suffice:



Traditionally, to enumerate and/or collect in Java, you'd use the file list() method with a filter that gives a yes/no indication for each file scanned to indicate whether it matched your selected pattern.

Alternatively these days, there are evil things you can do with Lamba expressions to the same effect, but I'll leave the details to others.
 
Marshal
Posts: 4084
554
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Take a look at Files#find.  Here's an example of what I had done before:
I shortend the paths with ...
 
Saloon Keeper
Posts: 9834
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So for Velan it might be something like this:

 
Tim Holloway
Saloon Keeper
Posts: 26541
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:So for Velan it might be something like this:



I think that the cleaner solution (assuming that regexes are ever "clean"!) is more like this:


That's the concept at least. Any resemblance between that and actual working Java regex code can be considered entirely accidental.
 
Carey Brown
Saloon Keeper
Posts: 9834
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tim, the OP made a typo in his problem description, "test" is a directory and not part of a file name.
 
Tim Holloway
Saloon Keeper
Posts: 26541
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Tim, the OP made a typo in his problem description, "test" is a directory and not part of a file name.



Ah well, just changes the regex a little.
 
Carey Brown
Saloon Keeper
Posts: 9834
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Holloway wrote:

Carey Brown wrote:Tim, the OP made a typo in his problem description, "test" is a directory and not part of a file name.



Ah well, just changes the regex a little.

Wouldn't that also mean getting a different segment of the path?
 
Tim Holloway
Saloon Keeper
Posts: 26541
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you'd just regex against the full path and not bother to rip out the filename part.

But I don't assume that's necessary, since upon more closely reviewing the problem description, it actually reads more like files in the form:

rootfolder/dynamic folders/test/test001.xml
rootfolder/dynamic folders/test/test002.xml
rootfolder/dynamic folders/test/test003.xml
rootfolder/dynamic folders/test/testabcd.xml

etc.
 
Ron McLeod
Marshal
Posts: 4084
554
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you filter based on the string representation of the full path, then you will need to consider the path separator character in your pattern (/ for Linux, \ for Windows).

 
Tim Holloway
Saloon Keeper
Posts: 26541
187
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then you're using the wrong Path method. Java supports a Universal (Unix-like) path notation where the separator is always a forward ("real") slash, and never a backslash (Windows) colon (Legacy Macintosh), '>' (Primos) or possibly non-terminal dot (IBM zOS). It's specifically to assist in "write-once/run-anywhere" and it's the format that I always promote. Since I have spent literally years as a full-time developer using Windows on the Desktop to produce apps to run on Linux and Solaris and I didn't want to have to debug code that worked differently on the desktop than in production.

Although as far as that goes, if you cannot resolve to an OS-independent form, use [\\/] as the path separator in the regex.
 
reply
    Bookmark Topic Watch Topic
  • New Topic