Solved

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

Posted on 2003-12-03
6
273 Views
Last Modified: 2010-04-01
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
0
Comment
Question by:EasyWaye
  • 2
  • 2
  • 2
6 Comments
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9872908
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.  
0
 
LVL 15

Accepted Solution

by:
jimmack earned 50 total points
ID: 9873017
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_"));
    }

}
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9873545
See I thought we weren't supposed to give full answers for homework questions???
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 15

Expert Comment

by:jimmack
ID: 9874642
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.
0
 

Author Comment

by:EasyWaye
ID: 9947301
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];
             }                          
          }                
0
 

Author Comment

by:EasyWaye
ID: 9947314
This is not a homework. Just is a small part of my program. I am developer but still new in Java. Thank you anyway.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rebasing and merging used in SCM 1 72
Fisheye tool 2 111
Spring MVC - sending raw charset to backend 3 203
iterator/ListIterator approach 17 27
The question appears often enough, how do I transfer my data from my old server to the new server while preserving file shares, share permissions, and NTFS permisions.  Here are my tips for handling such a transfer.
As a business owner, there are many things that keep you up at night. Profit margins, employee retention, human resource protocols, whether your product or service will remain competitive. When you own or manage a technology company that operates la…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question