java files

Java.IO.File canWrite() Filewriter in java Code With Examples

Table of Contents

Program

import java.io.*;

public class FileCanWrite 
{
	public static void main(String args[]) 
	{
		File f = new File("bin");

		if (f.canWrite())
                 {
			System.out.println("Can be written");
                 }
		else
                 {
			System.out.println("Cannot be written");
                 }
	}
}

Output

Can be written

Description

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.

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