Java File example to check a file is already present using exists


public boolean exists()

Tests whether the file or directory denoted by this abstract pathname exists.


Program:

package com.candidjava;
import java.io.File;

public class FileExists
{
	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


Description:

    The given file's presence is being checked by exists method. 


Returns:

true if and only if the file or directory denoted by this abstract pathname exists; false otherwise


Throws:

SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file or directory





Related Post

Comments


©candidjava.com