2:28 am - Tuesday March 28, 2017

To check a file is already present using exists() method

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This tutorial explains how To check a file is already present using exists() method,The java.io.File.exists() method tests the existence of the file or directory defined by this abstract pathname.

import java.io.File;</p>
public class CheckFileExist

{

public static void main(String[] args)

{

File f = new File("/home/mathan/Documents/add.txt");

File f1 = new File("/home/mathan/Documents");

String v, v1;

boolean bool = false;

v = f.getName();

v1 = f1.getName();

bool = f.exists();

if (bool)

{

System.out.println("File name: " + v);

}

bool = f1.exists();

if (bool)

{

System.out.print("Folder name: " + v1);

}

}

}

Output

      File name: add.txt

      Folder name: Documents

BACK

Filed in: Java Examples

Comments are closed.