1.當Java.io中,如果文件的操作的時候,判斷是否隱藏用File.ishiden()
判斷是否只讀,可用File.canWrite().
2. 當要設置是否是可讀或者是隱藏時,在java中除了提供File.setReadOnly()外,就無其他方法了。
所以我們必須到Dos環境下去設置,在java中用Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " +R")該方法可以實現。因為路徑file.getAbsolutePath()中可能會還有空格,所以必須用引號把它括起來,當作一個參數。這樣就可以實現了
(1)設置只讀Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " +R");
(2)設置可寫Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " -R");
(3)設置隱藏Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " +H");
(4)設置非隱藏Runtime.getRuntime().exec("attrib " + """ + file.getAbsolutePath()+ """+ " -H");
3.對於讀寫文件的一些操作源代碼
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 main(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);
// 緩沖寫入文件,並且可以通過轉義符或是bw.newLine();來換行
BufferedWriter bw = new BufferedWriter(fw);
// 將字符串寫入文件
bw.write(" 大家好!");
bw.newLine();
bw.write("本?是《JSP?程技巧》");
bw.newLine();
bw.write("?多多指教!");
bw.newLine();
bw.write("email:[email protected]");
bw.flush();
bw.close();
//在文件制定位置追加內容
RandomAccessFile rf = new RandomAccessFile("E:" + "\" + fileName, "rw");
rf.seek(rf.length());
rf.writeBytes(" zui jia de");
rf.close();
// 通過緩沖來讀文件
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");
}
}
}