JAVA IO - 設置文件爲只讀

import java.io.File;
import java.io.FileOutputStream;

public class ReadOnlyTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {
		File file = new File("C:\\work\\hello\\helloworld.txt");
		file.setReadOnly();

		try {
			FileOutputStream os = new FileOutputStream(file);
			for (int i = 0; i < 100000; i++) {
				os.write("This is a file size test".getBytes());
			}
			os.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		file.setWritable(true);
		
		try {
			FileOutputStream os = new FileOutputStream(file);
			for (int i = 0; i < 100000; i++) {
				os.write("This is a file size test".getBytes());
			}
			os.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		long length = file.length();
		System.out.println("File Size " + length / 1024 + "K");
		System.out.println("File Size " + length / 1024 / 1024 + "M");
	}

}
相關文章
相關標籤/搜索