Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

Determining if a File or Directory Exist

                         

This is a very simple problem. You just have to determine whether a file or directory exists or not. If it exists it returns true otherwise in the else condition, it retunes vice-versa.

The following program creates an object of File class and checks whether the given file exists or not by using exists() method of the class.

We have used following methods:

exists(): It checks whether the file or directory denoted by this pathname exists or not.

Code of this program is given below:

import java.io.*;

public class FileOrDirectoryExists{
  public static void main(String args[]){
    File file=new File("Any file name or 
         directory whether exists or not"
);
    boolean exists = file.exists();
    if (!exists) {
      // It returns false if File or directory does not exist
      System.out.println("the file or directory 
      you are searching does not exist : " 
+ exists);
        
    }else{
      // It returns true if File or directory exists
      System.out.println("the file or 
      directory you are searching does exist : " 
+ exists);
    }
  }
}

The output of the program is given below:

C:\java>java FileOrDirectoryExists
the file or directory you are searching does not exist : false

Download this example:

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

2 comments so far (
post your own) View All Comments Latest 10 Comments:

THanks,

WOrks great on my code.

Posted by C/ on Sunday, 06.1.08 @ 01:15am | #61613

Idid the same thing but it doesn't work it always give me an error:"cannot find symbol symbol : class file location: class ", I import the java.o.* class.
I wrote exactly like this:
File file = new file("/rep1/rep/74060-48.pdf");
boolean exists =file.exsists();
I also change slash to back slash and double them but no way, no thing work.
Please if you have an idea help me.
thanks.
(I work in a jsp page)

Posted by DEVSA on Friday, 11.2.07 @ 00:19am | #35349

Latest Searches:
standered jstl jar to
object oriented progra
web
array element
Photoshop Text Effects
reload image
breadcrumbs in jsp
struts tld downloas
Threads
criteria restrictions
Java Mysql
global variable in j
Diff bet MVC Model1 an
code for inserting val
how to override a vari
count
java bean insert conne
how to read txt file f
Create table and inser
Javascript Date and Ti
retriving multiple ima
GWT Servlet
jsf tree node set hr
creat
Passing Parameters in
requestDispatcher
program for factorial
FCKediter
java program to check
oracle
string variable
create xml using java
display arraylist
string array add eleme
how to use maps in jav
save image to file
struts menu
Flash Tips and Techniq
HWPF
tree
Servlet to update the
jsp export to pdf
how to use unique cons
JButton with image
jsp to microsoft sql
Hibernate API
Murachั??ะ??ั??à
find a replace bytes i
TCP buffer
date validation in jav
writing data to file
The JDBC(tm) Universal
ANTretrieve data from
JSP and Ajax for datab
string to integer
date validation in Jav
Creating a DSN for MyS
Photoshop Basic Shapes
notes applet
Math.random() example
SIMPLE JAVA SOURSE COD
Java code for video pl
standard deviation arr
calculator square root
Servlet Example To Dis
Photoshop Refrozen Eff
Vector
get method of from in
insert data to text fi
image upload
program to validate us
rational robot tool in
customise css in jsp
Math.random()
vector class
ascii
Call Cookies from Serv
struts.jar download
how to take two diment
decimal to ANSI
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.