📜  使文件只读的Java程序

📅  最后修改于: 2022-05-13 01:55:21.149000             🧑  作者: Mango

使文件只读的Java程序

只读是操作系统分配给文件的文件属性。当文件被标记为只读时,意味着它只能被打开或读取,不能更改文件名,不能重写或附加文件内容,也不能删除文件。

方法一:

使用 setLastModified 方法

为了使文件只读,使用文件类的 setReadOnly() 方法。此方法返回布尔值,用于检查任务(使文件只读)是否成功。如果方法返回的值为true,则表示任务成功,反之亦然。

参数:该函数不需要任何参数。

返回值:函数返回布尔数据类型。如果 File 对象可以设置为只读,则该函数返回 true,否则返回 false。

异常:如果该方法不允许对文件进行写访问,则此方法抛出SecurityException

Java
// Java program to make the file as read only
 
import java.io.File;
 
public class GFG {
   
    public static void main(String[] args)
    {
        // flag variable which contains the boolean
        // value returned by setReadOnly() function
        boolean flag;
       
        try {
           
            File file = new File("/home/mayur/GFG.java");
           
            // creates a new file
            file.createNewFile();
           
            // flag the file to be read-only
            flag = file.setReadOnly();
           
            System.out.println("Is File is read-only ? : "
                               + flag);
           
            // checking whether Is file  writable
            flag = file.canWrite();
            System.out.println("Is File is writable ? : "
                               + flag);
        }
       
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}


Java
import java.io.File;
 
public class ChangetoReadOnly {
 
    public static void main(String[] args)
    {
        try {
            File file = new File(
                "C://Users//sai mohan pulamolu//Desktop//test.txt");
           
            // making the file to read only mode
            file.setWritable(false);
           
            // check if the  file is writable or not
            // if not writable then it is readonly file.
            if (!file.canWrite()) {
                System.out.println(
                    "This File is read only.");
            }
            else {
                System.out.println(
                    "This File is writable.");
            }
        }
        catch (Exception e) {
            System.out.println(
                "Sorry unable to change to readonly mode.");
        }
    }
}


输出:

使文件只读的 Java 程序

方法二:使用 setWritable() 方法

这里通过将“false”作为参数传递给 setWritable() 方法,我们可以使文件只读。

以下代码将帮助您了解如何使用 setWritable() 使文件只读。

Java

import java.io.File;
 
public class ChangetoReadOnly {
 
    public static void main(String[] args)
    {
        try {
            File file = new File(
                "C://Users//sai mohan pulamolu//Desktop//test.txt");
           
            // making the file to read only mode
            file.setWritable(false);
           
            // check if the  file is writable or not
            // if not writable then it is readonly file.
            if (!file.canWrite()) {
                System.out.println(
                    "This File is read only.");
            }
            else {
                System.out.println(
                    "This File is writable.");
            }
        }
        catch (Exception e) {
            System.out.println(
                "Sorry unable to change to readonly mode.");
        }
    }
}

输出: