Home Java Files Methods isWritable() Java Files isWritable() Method 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:
boolean canEdit = Files.isWritable(report);
boolean canCreate = Files.isWritable(report.getParent()); Call What 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 public static boolean isWritable(Path path) Parameters Parameter Description pathThe file or folder to test.
Return Value Return Value Description trueIt exists and the JVM has write access. falseMissing, read-only, or unknown.
Important
No checked exception. On Linux the root user can write to read-only files, so it still sees true. On Windows a read-only file usually cannot be deleted either, so make it writable again before cleanup. Writes can still fail later (full disk, file locked), so keep the IOException handling.
Compare with Similar Methods Permission checks:
Java Examples 1. Toggle read-only 2. Files that do not exist yet 3. Skip read-only files 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.
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);
}
} Run with java IsWritableReadOnly. Same output on Windows, macOS, and Linux for a normal user; root on Linux would see true in all three lines.
Writable: true
After setWritable(false): false
After setWritable(true): true How It Works Writable by default. The owner can change the file it just wrote.Read-only everywhere. setWritable(false) sets the Windows read-only attribute or clears the Unix write bit, and isWritable turns false.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.
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);
}
} Run with java IsWritableMissing. The output is the same on Windows, macOS, and Linux.
export.csv before: false
its folder: true
export.csv after: true How It Works Missing is false. There is no export.csv yet, so there is nothing to be writable.Creating needs the folder. Adding a file changes the folder, so the useful question is whether the temp folder is writable, and it is.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.
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);
}
} Run with java IsWritableSave. Same output on Windows, macOS, and Linux for a normal user (root on Linux would overwrite locked.txt).
notes.txt: saved 5 bytes
locked.txt: skipped, read-only How It Works 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.New file saved. notes.txt does not exist, the temp folder is writable, and five bytes of "alpha" are written.Protected file skipped. locked.txt was made read-only, so the helper leaves its "keep" content alone.Frequently Asked Questions What does Files.isWritable return for a file that does not exist? false. To check whether you can create the file, test its parent folder with Files.isWritable(path.getParent()).
How do I make a file read-only in Java? 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.
Why is a read-only file still writable for root? On Linux the root user bypasses permission bits, and isWritable reports what the operating system would allow.
Can I delete a read-only file on Windows? 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.
Does Files.isWritable guarantee the write will succeed? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion