Solved

How to check file's first name?

Posted on 2006-06-23
11
251 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java string replace 11 77
going to wrong jsp page 2 69
servlet and mdb, jms error 1 81
using executorService 3 19
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

734 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