FileOutputStream fos = new FileOutputStream("output.txt"); FileDescriptor fd = fos.getFD(); // Use the FileDescriptor object to set the file to read-only fd.sync(); // Ensure all file data is written to disk fd = new FileOutputStream("output.txt").getFD(); fd.canWrite(false); // Make the file read-only
FileOutputStream fos = new FileOutputStream("output.txt"); FileDescriptor fd = fos.getFD(); // Use the FileDescriptor object to obtain file status information fd.sync(); // Ensure all file data is written to disk fd = new FileOutputStream("output.txt").getFD(); System.out.println("File size: " + fd.length()); System.out.println("Last modified: " + new Date(fd.lastModified()));In this example, we create a new FileOutputStream and get its associated FileDescriptor object. We then call the sync method on the FileDescriptor to ensure all data is written to disk. We then create a new FileOutputStream and get its associated FileDescriptor, which we use to obtain file status information by calling the length and lastModified methods. The java.io package library is used in both examples.