Solved

How to check file's first name?

Posted on 2006-06-23
11
242 Views
Last Modified: 2010-03-31
I have a situation in which I only have a file's first name(NOT EXTENSION) like 'ms0045' that is coming from database.
Now there may or may not be a file in a known directory with that name.  I want to check if 'ms0045' exists in the directory with any extension or not.

how can i do that.
0
Comment
Question by:jaipur07
  • 5
  • 4
  • 2
11 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 16969624
Just iterate the directory

boolean found = false;
String[] files = new File(x).list();
for(int i = 0;i < files.length && !found;i++) {
      found = files[i].getName().startsWith(searchName);
 
}
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 250 total points
ID: 16969634
File f = new File ( "filepath/filename.extension" ) ;
String fullFileName = f.getName () ;
String fileName = fullFileName.substring ( 0, fullFileName.lastIndexOf ( "." ) ) ;

should do it, I guess....
0
 

Author Comment

by:jaipur07
ID: 16969640
startsWith will return all the names like ms00451212', 'ms00455555'

isn't it?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 30

Expert Comment

by:Mayank S
ID: 16969643
(I mean just getting the name of a file - not searching)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16969665
CEHJ's and mine combined should do both.
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16969671
>> String[] files = new File(x).list();

Use listFiles () if you want to check for only files, not directories.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16969674
>>startsWith will return all the names like ms00451212', 'ms00455555'

Yes. If that's a problem you can do


found = files[i].getName().startsWith(searchName) && searchName.equals(files[i].getName().substring(0, files[i].getName().lastIndexOf('.'));
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16969680
(You can remove the first condition before &&)
0
 

Author Comment

by:jaipur07
ID: 16969737
let me try and will get back to yo usoon
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16969807
Note that there is small optimization in my approach that you don't have to call getName () multiple times saving a method-call :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16969830
:-)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

856 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