Creates a file and sets it to read-only. : File « JDK 6 « Java






Creates a file and sets it to read-only.

 

import java.io.File;
import java.io.IOException;
public class FileAttributesDemo {

    public static void main (String[] args) throws IOException {
        // Create a new file, by default canWrite=true,
       readonly=false
        File file = new File ("test.txt");
        if (file.exists ()) {
            file.delete ();
        }
        file.createNewFile ();
        System.out.println ("Before. canWrite?" + file.canWrite ());

        // set to read-only, atau canWrite = false */
        file.setWritable (false);
        System.out.println ("After. canWrite?" + file.canWrite ());
    }
}

        








Related examples in the same category

1.List all roots
2.Getting a Proper URL from a File Object
3.Get the free space
4.Get the usable space
5.Get the total space
6.File Class Enhancements
7.Create a file and change its attribute to readonly