Home > File > Thiết đặt file chỉ đọc trong Java

Thiết đặt file chỉ đọc trong Java

Trong Java, để thiết đặt cho 1 file chỉ đọc, hãy dùng phương thức setReadOnly(). Kể từ JDK 1.6 có cung cấp phương thức setWritable() để thiết đặt file có thể ghi trở lại.

package quyetdv.java.javaio.file;
import java.io.File;
import java.io.IOException;
public class FileReadAttributeExample {
    public static void main(String[] args) throws IOException {
        File file = new File("test.txt");
        // mark this file as read only, since jdk 1.2
        file.setReadOnly();
        if (file.canWrite()) {
            System.out.println("This file is writable");
        } else {
            System.out.println("This file is read only");
        }
        // revert the operation, mark this file as writable, since jdk 1.6
        file.setWritable(true);
        if (file.canWrite()) {
            System.out.println("This file is writable");
        } else {
            System.out.println("This file is read only");
        }
    }
}

 

Advertisements
Categories: File
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: