Get the filename that starts with "tax_" in a specified path

All Experts,

        I have no idea on how to get a file name that starts with a variable value in a specified path.

       For an example:

       I have a string variable :  String val = "tax_";

       I have a path : String up_paths = "C:/foldername";

       A file named tax_file.txt exists in "C:/foldername".

       The program is able to get the file name that starts with "tax_" in the path C:/foldername. Then the "tax_file.txt" will return.

EasyWaye
EasyWayeAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

grim_toasterCommented:
I would suggest looking into the File API, in particular the listFiles(), and the list(filter) methods to get the files contained within a directory.  
jimmackCommented:
public class MyClass implements FileFilter
{
    public void getTaxFiles()
    {
        File dirToList = "C:\\foldername";
        File[] taxFiles = dirToList.listFiles(this);
        // taxFiles is an array of files that start with "tax_"
    }
    .
    .
    public boolean accept(File filter)
    {
        return (filter.getName().startsWith("tax_"));
    }

}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
grim_toasterCommented:
See I thought we weren't supposed to give full answers for homework questions???
Exploring SharePoint 2016

Explore SharePoint 2016, the web-based, collaborative platform that integrates with Microsoft Office to provide intranets, secure document management, and collaboration so you can develop your online and offline capabilities.

jimmackCommented:
Hi grim_toaster,

  If this is a homework question, then it looks like a very small part of it.  It doesn't "smell" too much like the typical homework questions that we get here... EasyWaye is only asking for some specific details and isn't offering 500 points ;-)

  If you thought that it was homework, then perhaps you should have mentioned this in your first comment.
EasyWayeAuthor Commented:
Thank you. I get the answer before you give me the answer. I am using different logic. But, seems like your one better.

     String filetype = "tax"; //actually filetype is a passing parameter
     String [] directoryList = theFile.list();                    
     int num = 0;
     num = filetype.length();
     String val = "";
     String filename = "";
     
           for (int i=0; i < directoryList.length; i++) {
           val=directoryList[i].substring(0,num);
             
             if (val.equals(filetype)) {
         
             filename = directoryList[i];
             }                          
          }                
EasyWayeAuthor Commented:
This is not a homework. Just is a small part of my program. I am developer but still new in Java. Thank you anyway.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JSP

From novice to tech pro — start learning today.