Java File.setReadonly()方法

2019-10-16 22:16:39

Java Java File.setReadOnly()方法具有以下語法。

public boolean setReadOnly()

範例

在下面的程式碼顯示如何使用File.setReadOnly()方法。

import java.io.File;

public class Main {
  public static void main(String[] args) {
    File f = new File("test.txt");

    // set file as read only
    boolean bool = f.setReadOnly();

    System.out.println("setReadonly() succeeded?: " + bool);

    // checks whether the file is writable
    bool = f.canWrite();

    System.out.print("Is file writable?: " + bool);

  }
}

上面的程式碼生成以下結果。

setReadonly() succeeded?: true
Is file writable?: false