HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / I/O / Java – Make a File Read Only

Java – Make a File Read Only

For making a file read-only in Java, we can use any one of the given below APIs.

  1. java.io.File.setReadOnly()
  2. java.io.File.setWritable(false)
  3. Runtime.getRuntime().exec()

The Runtime.getRuntime().exec() is platform-specific because of the system command we need to pass to it as parameter. The other two methods will work fine in most cases.

If you want to change Linux/Unix specific file properties e.g. using “chmod 775” then Java does not provide any way to do it.

1. Make file read only with File.setReadOnly()

The setReadOnly() method marks the file or directory specified in path so that only read operations are allowed.

private static void readOnlyFileUsingNativeCommand() throws IOException 
{
	File readOnlyFile = new File("c:/temp/testReadOnly.txt");
	
	//Mark it read only
	readOnlyFile.setReadOnly();
}

2. Use java.io.File.setWritable(false) method

setWritable() is a convenience method to set the owner’s write permission for this abstract pathname.

private static void readOnlyFileUsingSetWritable() throws IOException 
{
	File readOnlyFile = new File("c:/temp/testReadOnly.txt");

	//Mark it read only
	readOnlyFile.setWritable(false);
}

3. Execute a native command (platform dependent)

exec() executes the specified command and arguments in a separate process in the operating system.

private static void readOnlyFileUsingSetReadOnly() throws IOException 
{
	File readOnlyFile = new File("c:/temp/testReadOnly.txt");

	//Mark it read only in windows
	Runtime.getRuntime().exec("attrib " + "" + readOnlyFile.getAbsolutePath() + "" + " +R");
}

Happy Learning !!

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Comments are closed on this article!

Search Tutorials

Java IO

  • Java IO Introduction
  • Java How IO works?
  • Java IO vs NIO
  • Java Create File
  • Java Write to File
  • Java Append to File
  • Java Read File
  • Java Read File to String
  • Java Read File to Byte[]
  • Java Make File Read Only
  • Java Copy File
  • Java Copy Directory
  • Java Delete Directory
  • Java Current Working Directory
  • Java Read/Write Properties File
  • Java Read File from Resources
  • Java Read File from Classpath
  • Java Read/Write UTF-8 Data
  • Java Check if File Exist
  • Java Create Temporary File
  • Java Write to Temporary File
  • Java Delete Temporary File
  • Java Read from Console
  • Java Typesafe input using Scanner
  • Java Password Protected Zip
  • Java Unzip with Subdirectories
  • Java Generate SHA/MD5
  • Java Read CSV File
  • Java InputStream to String
  • Java String to InputStream
  • Java OutputStream to InputStream
  • Java InputStreamReader
  • Java BufferedReader
  • Java FileReader
  • Java LineNumberReader
  • Java StringReader
  • Java FileWriter
  • Java BufferedWriter
  • Java FilenameFilter
  • Java FileFilter

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)