Java File example to check the file can read and write using canRead & canWrite


public boolean canRead()
Tests whether the application can read the file denoted by this abstract pathname. On some platforms it may be possible to start the Java virtual machine with special privileges that allow it to read files that are marked as unreadable. Consequently this method may return true even though the file does not have read permissions.

public boolean canWrite()
Tests whether the application can modify the file denoted by this abstract pathname. On some platforms it may be possible to start the Java virtual machine with special privileges that allow it to modify files that are marked read-only. Consequently this method may return true even though the file is marked read-only.

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

public class FileCanwrite
{
	public static void main(String[] args)
	{
		File f = new File("/home/mathan/Documents/abcd.txt");
		if (f.canRead())
		System.out.println("Can read  abcd.txt:");
		if (f.canWrite())
		System.out.print("Can write abcd.txt ");
	}
}

Output:
Can read  abcd.txt
Can write abcd.txt

Description:
        The file is being verified whether has it been able to be read and write.

canRead()
Returns:
true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise

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

canWrite()
Returns:
true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.

Throws:
SecurityException - If a security manager exists and its SecurityManager.checkWrite(java.lang.String) method denies write access to the file



Related Post

Comments


©candidjava.com