Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Tip: Making a File Read-only Using Java

Explore how the setReadOnly() method works in Java.


advertisement

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 java.io.File;

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();
     }
   }
}

 

Visit the DevX Tip Bank

 





   
Sridhar M S. is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date