aspose file tools
The moose likes Java in General and the fly likes test whether a file exists or not Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "test whether a file exists or not" Watch "test whether a file exists or not" New topic
Author

test whether a file exists or not

Dave Mere
Ranch Hand

Joined: Oct 25, 2004
Posts: 37
Hello all,

I'm developing an application designed to return records along with any associated images. The images will be named after the records primary key; ie "102.jpg". I'm going to display the full record information on one jsp and provide a link to another jsp to display the image, via a button/link whatever.

The problem is that not all records have associated images. I have to engineer the system such that the link appears ONLY when an image is present in the img directory. How can I test at runtime whether file 102.jpg is present?

Thanks!
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Hmmm... where in the API do you suppose there would be a method to tell if a file exists?
And what would be its name?


There is no emoticon for what I am feeling!
Raymond Villeno
Ranch Hand

Joined: Jun 21, 2005
Posts: 63
are you using a database in your application?


"Work Smart Not Hard"
Dave Mere
Ranch Hand

Joined: Oct 25, 2004
Posts: 37
Right . . . I've found plenty of reference to a FindFile class/object/something but it doesn't appear to be available to my application as it stands. How do I get access to this?

thanks!
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12952
    
    3

Look at the API documentation of class java.io.File.

You don't need a FindFile class/object/something (that's not part of the standard API).


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Shobhana S Raman
Greenhorn

Joined: Dec 09, 2005
Posts: 1
Hi,

I think from the primary key you can get the file name. Then when you use the following code,
File f = new File("File name");
if(f.exists()) {
//your file exists
} else {
//file doesn't exist.
}

Hope this clears your doubt.

Regards
Shobhana
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 10043
    
    6

Davemere,

I guess you missed it on your way in, buy your display name doesn't quite meet up with our Naming Policy. In short, you must have a real (sounding) first name, a space, and a last name.

Would you be so kind as to update yours here?

thanks, pardner!!!


Never ascribe to malice that which can be adequately explained by stupidity.
Dave Mere
Ranch Hand

Joined: Oct 25, 2004
Posts: 37
Jesper, Shobhana, thanks for your informative replies - it's all working now!

Jeff - whereas this solution is obviously clear to one as Sun-certified as yourself, sometimes they are less so to those of us with less experience. Half a reply isn't much use when you're on a deadline.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Dave,

I was trying to follow the Java Ranch credo of "Give a man a fish... Teach a man to fish..."
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12324
    
    1
Half a reply isn't much use when you're on a deadline.

Following Dave's advice would have been a * of alot faster than waiting for a reply. Get familiar with the basic Java library if you are using java.io classes it should be obvious that they use the java.io.File class to represent the disk file.
Bill


Java Resources at www.wbrogden.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: test whether a file exists or not
 
Similar Threads
What is best way to name jpg images in a web application?
JApplet resources
Image Tag Library
Automatic building in Eclipse JEE Galileo
Displaying image in struts