Creates a file and sets it to read-only -- Java

Java programming topics
Post Reply
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Creates a file and sets it to read-only -- Java

Post by Nipuna » Wed Apr 11, 2012 9:26 am

Code: Select all

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

User avatar
SevenZero
Major
Major
Posts: 263
Joined: Sun Nov 01, 2009 8:37 pm

Re: Creates a file and sets it to read-only -- Java

Post by SevenZero » Wed Apr 11, 2012 6:31 pm

Very nice Nipuna. Keep up the good work.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Creates a file and sets it to read-only -- Java

Post by Nipuna » Wed Apr 11, 2012 9:44 pm

Thank you,

But these codes are not my ones, I found them online. You know if these are mine, I would have yelled so many thing that I did when I was coding it :)
Post Reply

Return to “Java Programming”