We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Checking for the presence of a data file
Page Index Toggle Pages: 1
Checking for the presence of a data file (Read 352 times)
Checking for the presence of a data file
Oct 29th, 2009, 11:32pm
 
I have a piece of code that tries to load an image file, but the image may or may not be present. I wrote my code this way so that I can change the appearance of my application just by dropping in image files (no code changes needed) but the loadImage() method puts out error messages when the file isn't present, even though it's not an error in this case.

I tried using the dataFile() method:

if (dataFile(imageName).exists()) {
 ...
}


but this doesn't work in an applet because the applet doesn't have enough permissions to call the exists() method.

I found a workaround, namely:

 boolean dataFileExists(String filename) {
   InputStream stream = createInput(filename);
   if (stream != null) {
     try {
       stream.close();
     }
     catch (IOException e) {
       e.printStackTrace();
     }
     return true;
   }
   return false;
 }

 if (dataFileExists(imageFile)) {
   ...
 }


Maybe this is useful for someone else too...
Page Index Toggle Pages: 1