/**
* Set file attributes.
*
* To use the program, specify the name of the file
* on the command line. for example, to set the attributes
* for a file called test.tst, use the following command line:
*
* java SetFileAttributes test.tst
*
*/
package FileIO;
import java.io.*;
import java.util.*;
/**
* @author Administrator
*
*/
public class SetFileAttributes {
/**
* Show a file's read/write status.
*/
public static void rwStatus(File f) {
if (f.canRead())
System.out.println(" Readable");
else
System.out.println(" Not Readable");
if (f.canWrite())
System.out.println(" Writable");
else
System.out.println(" Not Writable");
}
/**
* @param args
*/
public static void main(String[] args) {
// First make sure that a file has been specified.
if (1 != args.length) {
System.out.println("Usage: SetFileAttributes filename");
return;
}
File f = new File(args[0]);
// Confirm that the file exists.
if (!f.exists()) {
System.out.println("File not found.");
return;
}
// Display original read/write status and time stamp.
System.out.println("Original read/write permission and time: ");
rwStatus(f);
System.out.println(" Last modified on " + new Date(f.lastModified()));
System.out.println();
// Update the time stamp.
long t = Calendar.getInstance().getTimeInMillis();
if (!f.setLastModified(t))
System.out.println("Can't set time.");
// Set the file to read-only.
if (!f.setReadOnly())
System.out.println("Can't set to read-only.");
System.out.println("Modified read/write permission and time: ");
rwStatus(f);
System.out.println(" Last modified on " + new Date(f.lastModified()));
System.out.println();
// Return to read/write status.
if (!f.setWritable(true, false))
System.out.println("Can't return to read/write.");
System.out.println("Read/Write permissions are now: ");
rwStatus(f);
}
}
SetFileAttributes
最新推荐文章于 2023-03-02 09:23:58 发布