Create a file and change its attribute to readonly : File « File « Java Tutorial






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








11.2.File
11.2.1.The File Class
11.2.2.Testing and Checking File Objects: file name and path
11.2.3.Providing a URI for a remote file
11.2.4.isAbsolute(): Returns true if the File object refers to an absolute pathname, and false otherwise.
11.2.5.getParent(): Returns the name of the parent directory of the file or directory represented
11.2.6.getParentFile(): Returns the parent directory as a File object, or null if this File object does not have a parent.
11.2.7.toString(): Returns a String representation of the current File object
11.2.8.exists(): Returns true if it exists and false otherwise
11.2.9.isDirectory(): Returns true if it is an existing directory and false otherwise
11.2.10.isFile(): Returns true if it is an existing file and false otherwise
11.2.11.isHidden(): Returns true if it is hidden and false otherwise
11.2.12.canRead(): Returns true if you are permitted to read the file and false otherwise.
11.2.13.canWrite(): Returns true if you are permitted to write to the file and false otherwise.
11.2.14.getAbsolutePath(): Returns the absolute path for the directory or file referenced by the current File object
11.2.15.getAbsoluteFile(): Returns a File object containing the absolute path for the directory or file referenced by the current File object.
11.2.16.list(): Returns a string array containing the children files and directories
11.2.17.listFiles(): Returns a File array containing the children files and directories
11.2.18.length(): Returns the length of current file in long
11.2.19.Create a human-readable file size
11.2.20.lastModified(): Returns last modified time in milliseconds since midnight on 1st January 1970 GMT
11.2.21.renameTo(File path): Rename a file or directory
11.2.22.setReadOnly(): Sets the file as read-only and returns true if the operation is successful
11.2.23.Filtering a File List
11.2.24.mkdir(): Creates a directory
11.2.25.mkdirs(): Creates a directory including any parent directories
11.2.26.createNewFile(): Creates a new empty file
11.2.27.createTempFile(String prefix, String suffix, File directory): a static method that creates a temporary file
11.2.28.delete(): delete the file or directory
11.2.29.Display File class constants and test some methods
11.2.30.deleteOnExit(): delete file or directory when the program ends
11.2.31.File Class Enhancements
11.2.32.Creates a file and sets it to read-only.
11.2.33.Create a file and change its attribute to readonly
11.2.34.List all roots
11.2.35.Get the free space
11.2.36.Get the usable space
11.2.37.Get the total space
11.2.38.Set file attributes.
11.2.39.Change a file attribute to writable
11.2.40.Change a file attribute to read only
11.2.41.Getting a Proper URL from a File Object
11.2.42.Get icon for file type
11.2.43.Get all xml files by file extension
11.2.44.Get file extension name
11.2.45.File.getCanonicalFile() converts a filename path to a unique canonical form suitable for comparisons.