All Things Techie With Huge, Unstructured, Intuitive Leaps

Java - determine an image or file exists

One of our programmers asked me how to determine if an image or a file exists last week, so I thought that I would post this code snippet for anyone else that needs a reminder.

String thumbnail = <put the name of image or file here>
try{
   File iFile = new File(config.getServletContext().getRealPath("/")
    + "albums/" + thumbnail);  //we keep all of our images in a directory called albums
   
   if (iFile.exists()) {
    String thisImage = "albums/"+ thumbnail;
     }  else {
        String thisImage = "albums/"+ "noImage.jgp";
     }
}
catch (Exception e)
{
  System.out.println("Can't find image.");
}


Hope this helps someone.

No comments:

Post a Comment