1

Is there any way to check image exist in a particular location except the following code?This code is taking too much time.

  static boolean isImage(String image_path){  
    InputStream input = null;
    try{

        URL url = new URL(image_path);
        input = url.openStream();
        return  true;

    }catch(Exception ex){
        return  false;
    }

  }
Baby
  • 5,062
  • 3
  • 30
  • 52
amitguptageek
  • 537
  • 3
  • 13
  • [Check if file exists on remote server using its URL](http://stackoverflow.com/questions/4596447/check-if-file-exists-on-remote-server-using-its-url) and [How do I check if a file exists in Java?](http://stackoverflow.com/questions/1816673/how-do-i-check-if-a-file-exists-in-java) – Baby Feb 06 '15 at 07:21
  • I don't have permission and exist() method returns true if file exist and have permission @ Jonjongot? – amitguptageek Feb 06 '15 at 07:24
  • if you don't have permission on it, it will throw `SecurityException`. means if you get this exception, the file is exist. – Baby Feb 06 '15 at 07:34
  • @ Jonjongot I am getting false even if file exists not SecurityException. – amitguptageek Feb 06 '15 at 07:53
  • not sure if you get what i'm saying or I don't understand you. Try add one more catch block just before your catch block above `catch(SecurityException se){ return true; }` – Baby Feb 06 '15 at 07:57

1 Answers1

2

If you just want to check if the file exists:

static boolean isImage(String image_path){  
    try{
        File f = new File(image_path);
        return  f.exists();

    }catch(Exception ex){
        return  false;
    }
}
StephaneM
  • 4,779
  • 1
  • 16
  • 33