Making a File Read-only Using Java

Making a File Read-only Using Java

Files have multiple properties and they are interesting to use. In fact, your own file system can be developed using them. Create a file named PATH.txt and try this program twice to see how the setReadOnly() method works. Explore and enjoy!

import;public class ReadOnlyFile {   public static void main(String[] args)    {      //File to choose     String fileNameWithPath = "C:/PATH.txt";     File file = new File(fileNameWithPath);           System.out.println("File is writeable: " + file.canWrite());     //If the file is writable, then making it read-only     if (file.canWrite())     {        System.out.println("File is writable. Making it read-only now");        file.setReadOnly();     }   }}
See also  Java Developer Jobs: Your Guide to Landing the Perfect Position in 2024

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist