Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7411805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:26:38+00:00 2026-05-29T06:26:38+00:00

Overview I’m in the process of porting a simple utility app over from C#

  • 0

Overview

I’m in the process of porting a simple utility app over from C# into Java and as part of this I’m writing some helper methods for items where I prefer C#’s semantics. Once such case is Directory.GetFiles.

Current Code

import java.io.File;
import java.io.FilenameFilter;

public class Directory {
    public static String[] GetFiles(String path) {        
        File directory = new File(path);
    
         return directory.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String filename) {
                return new File(dir, filename).isFile();
            }
        });
     }
}

Question

Whilst the above all seems fine, and replicates one of the GetFiles overloads, I’m stuck on how best to write a method that replicates the functionality of C#’s Directory.GetFiles(String, String).

This method should take a path string, as well as a searchPattern, which is used to return only files matching that particular pattern.

For example, each of the following should work:

// Used to get all JavaScript files
Directory.GetFiles("~/Documents/", "*.js");

// Get all CSS files in the styles sub-folder.
Directory.GetFiles("~/Documents/", "styles/*.css"); 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:26:38+00:00Added an answer on May 29, 2026 at 6:26 am

    You could modify the pattern by placing a period before each asterisk and question mark, and then use this as a regular expression to determine which files you should return. So, you could write something like

    public static String[] GetFiles(final String path, final String searchPattern) {
        final Pattern re = Pattern.compile(searchPattern.replace("*", ".*").replace("?", ".?"));
        return new File(path).list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return new File(dir, name).isFile() && re.matcher(name).matches();
            }
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Overview I am using CompositeWPF to create an app using C#. This really should
I have a overview page containing a list with some links from which multiple
Overview I am writing a Ruby program that uses data from mysql queries to
Overview: I have a Silverlight 4 app where I am seeing problematic behavior from
Overview: I have two text boxes at the top of this form for inputting
Overview: I'm working on some windows service using Visual Basic 2010, which deployed on
Overview: I am trying to avoid a race condition with accessing an IndexedDB from
Overview From time to time in jQuery I want to check a condition of
Overview of My Situation: My task is to read strings from a file, and
Overview I'm looking to analyse the difference between two characters as part of a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.