java设置文件属性

package com.wujc.hidden;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 
 *  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");


 * @author wjc
 *
 */
public class HiddenTest {

	/**
	 * @param args
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
	
		test1();
		
		
	}
	
	public static void test1() throws IOException{
		
File f = new File("D:/list/web1.xml");
		
		String sets = "attrib -H \"" + f.getAbsolutePath() + "\"";
		System.out.println(sets);

		
		Runtime.getRuntime().exec(sets);

	}
	
	public static void test() throws IOException{
		
File f = new File("D:/list/web1.xml");
		
		String sets = "attrib +H \"" + f.getAbsolutePath() + "\"";
		System.out.println(sets);

		
		Runtime.getRuntime().exec(sets);
		
		PrintWriter pt = new PrintWriter(new FileWriter(f));
    	pt.println("i love you ");
    	pt.close();

	}
}

你可能感兴趣的:(java,xml,F#,dos)