1. When the java.io, if the file operation, to determine whether to hide with File.ishiden ()
Determines whether read-only, available file.canwrite ().
2. In addition to providing file.setreadonly () in Java, there is no other way to set whether it is readable or hidden.
So we have to go to the DOS environment to set up, in Java with Runtime.getruntime (). EXEC ("attrib" + "" + file.getabsolutepath () + "" + "+r") This method can be implemented. Because there may be spaces in the path File.getabsolutepath (), you must enclose it in quotation marks as a parameter. So that we can achieve the
(1) Set read-only Runtime.getruntime (). EXEC ("attrib" + "" "+ file.getabsolutepath () +" "" + "+r");
(2) Set writable Runtime.getruntime (). EXEC ("attrib" + "" "+ file.getabsolutepath () +" "" + "-r");
(3) Set the hidden Runtime.getruntime (). EXEC ("attrib" + "" "+ file.getabsolutepath () +" "+" +h ");
(4) Set the Runtime.getruntime (). EXEC ("attrib" + "" "+ file.getabsolutepath () +" "+"-H ");
3. Some operating source code for reading and writing files
Package Filecontrol
Import java.io.BufferedReader;
import java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileReader;
Import Java.io.FileWriter;
Import java.io.IOException;
Import Java.io.RandomAccessFile;
public class Filecontrol {
private static String fileName,
private static file file;
public static void M Ain (string[] args) {
try {
FileName = "Exercise.java";
File = new file ("E:" + "\", fileName);
if (file.exists ()) {
//File.delete ();
} else {
File.createnewfile ();
}
if (file.isdirectory ()) {
System.out.println ("This file is directory");
}
if (File.isfile ()) {
System.out.println ("This file is a file");
}
FileWriter fw = new FileWriter ("E:" + "\" + fileName);
//buffer is written to the file and can be wrapped by an escape character or bw.newline ();
BufferedWriter bw = new BufferedWriter (fw);
//write string to file
Bw.write ( "Hello, everyone!" ");
Bw.newline ();
Bw.write ("This is the JSP process tip");
Bw.newline ();
Bw.wriTE ("Advice!") ");
Bw.newline ();
Bw.write ("Email:stride@sina.comqianyf");
Bw.flush ();
Bw.close ();
//Append content in file location
Randomaccessfile RF = new Randomaccessfile ("E:" + "\" + fileName, "RW");
Rf.seek (Rf.length ());
Rf.writebytes ("Zui Jia de");
Rf.close ();
//Read file by buffering
FileReader fr = new FileReader ("E:" + "\" + fileName);
BufferedReader br = new BufferedReader (FR);
String linedata = Br.readline ();
while (null!= linedata) {
System.out.println (linedata);
Linedata = Br.readline ();
}
} catch (IOException ee) {
System.out.println ("System Exception");
}
}
}