Java Files isWritable() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

In Java, Files.isWritable(path) returns true when the file exists and the current user may modify it. A read-only file, a missing file, or a file whose status cannot be checked returns false.

On Windows it looks at the read-only attribute and access rights; on Linux and macOS at the permission bits. To ask "may I create a new file here?", check the parent folder instead.

A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.isWritable(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Pass the path; there are no options:

Java
boolean canEdit = Files.isWritable(report);
boolean canCreate = Files.isWritable(report.getParent());
CallWhat it does
Files.isWritable(p)Exists and may be written.
p.toFile().setWritable(false)Makes a file read-only (Windows and Unix).
Files.isReadable(p)Read permission instead.

Since Java 7. The examples use java.io.File.setWritable because it works on every system: it sets the read-only attribute on Windows and removes the owner's write bit on Linux and macOS.

Library declarations
Java
public static boolean isWritable(Path path)

Parameters

ParameterDescription
pathThe file or folder to test.

Return Value

Return ValueDescription
trueIt exists and the JVM has write access.
falseMissing, read-only, or unknown.

Compare with Similar Methods

Permission checks:

MethodHow it differs
isReadable()Read access.
setAttribute()Can set dos:readonly on Windows.
write()The operation the check usually protects.

Java Examples

Example 1 — Make a File Read-Only and Writable Again

Watch isWritable follow the read-only state, then restore it so the file can be deleted.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class IsWritableReadOnly {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path report = dir.resolve("report.txt");
        Files.write(report, "Q1 numbers".getBytes(StandardCharsets.UTF_8));
        System.out.println("Writable: " + Files.isWritable(report));
        report.toFile().setWritable(false);
        System.out.println("After setWritable(false): " + Files.isWritable(report));
        report.toFile().setWritable(true);
        System.out.println("After setWritable(true): " + Files.isWritable(report));
        Files.delete(report);
        Files.delete(dir);
    }
}

How It Works

  1. Writable by default. The owner can change the file it just wrote.
  2. Read-only everywhere. setWritable(false) sets the Windows read-only attribute or clears the Unix write bit, and isWritable turns false.
  3. Undo before deleting. Making it writable again restores true and lets Files.delete succeed on Windows too.

Example 2 — Can I Create It? Ask the Folder

A file that does not exist yet is never writable; the parent folder answers whether you can create it.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class IsWritableMissing {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path export = dir.resolve("export.csv");
        System.out.println("export.csv before: " + Files.isWritable(export));
        System.out.println("its folder: " + Files.isWritable(export.getParent()));
        Files.write(export, "id,name".getBytes(StandardCharsets.UTF_8));
        System.out.println("export.csv after: " + Files.isWritable(export));
        Files.delete(export);
        Files.delete(dir);
    }
}

How It Works

  1. Missing is false. There is no export.csv yet, so there is nothing to be writable.
  2. Creating needs the folder. Adding a file changes the folder, so the useful question is whether the temp folder is writable, and it is.
  3. After writing. Once the file exists, the program owns it and the answer becomes true.

Example 3 — A Save Helper That Respects Read-Only Files

Before overwriting, check the file (or, for a new file, its folder) and skip files marked read-only.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class IsWritableSave {
    static String save(Path p, String text) throws IOException {
        boolean ok = Files.exists(p) ? Files.isWritable(p) : Files.isWritable(p.getParent());
        if (!ok) {
            return p.getFileName() + ": skipped, read-only";
        }
        Files.write(p, text.getBytes(StandardCharsets.UTF_8));
        return p.getFileName() + ": saved " + Files.size(p) + " bytes";
    }

    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path notes = dir.resolve("notes.txt");
        Path locked = dir.resolve("locked.txt");
        Files.write(locked, "keep".getBytes(StandardCharsets.UTF_8));
        locked.toFile().setWritable(false);
        System.out.println(save(notes, "alpha"));
        System.out.println(save(locked, "overwrite"));
        locked.toFile().setWritable(true);
        Files.delete(notes);
        Files.delete(locked);
        Files.delete(dir);
    }
}

How It Works

  1. Two different questions. For an existing file the helper asks about the file; for a new one it asks about the folder that will hold it.
  2. New file saved. notes.txt does not exist, the temp folder is writable, and five bytes of "alpha" are written.
  3. Protected file skipped. locked.txt was made read-only, so the helper leaves its "keep" content alone.

Frequently Asked Questions

false. To check whether you can create the file, test its parent folder with Files.isWritable(path.getParent()).
path.toFile().setWritable(false) works on all systems. On Windows you can also use Files.setAttribute(path, "dos:readonly", true); on Linux and macOS Files.setPosixFilePermissions.
On Linux the root user bypasses permission bits, and isWritable reports what the operating system would allow.
Usually not with Files.delete; it fails with an AccessDeniedException. Make it writable first. On Linux and macOS deleting depends on the folder's permissions, not the file's.
No. The disk can be full, the file can be locked, or permissions can change after the check, so keep catching IOException.

Next: Files.lines()

Read a text file lazily as a Stream of lines; close it with try-with-resources (Java 8+).

lines() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

13 people found this page helpful