IO流——File類

import java.io.*;

/*
File類常見方法:
1,創建。
	boolean createNewFile():在指定位置創建文件,如果該文件已經存在,則不創建,返回false。
						和輸出流不一樣,輸出流對象一建立創建文件。而且文件已經存在,會覆蓋。

	boolean mkdir():創建文件夾。
	boolean mkdirs():創建多級文件夾。
2,刪除。
	boolean delete():刪除失敗返回false。如果文件正在被使用,則刪除不了返回falsel。
	void deleteOnExit();在程序退出時刪除指定文件。


3,判斷。
	boolean exists() :文件是否存在.
	isFile():
	isDirectory();
	isHidden();
	isAbsolute();

4,獲取信息。
	getName():
	getPath():
	getParent():

	getAbsolutePath() 
	long lastModified() 
	long length() 


	


*/


class FileDemo 
{
	public static void main(String[] args) throws IOException
	{
		method_5();
	}

	public static void method_5()
	{
		File f1 = new File("c:\\Test.java");
		File f2 = new File("d:\\hahah.java");

		sop("rename:"+f2.renameTo(f1));

	}

	public static void method_4()
	{
		File f = new File("file.txt");

		sop("path:"+f.getPath());
		sop("abspath:"+f.getAbsolutePath());
		sop("parent:"+f.getParent());//該方法返回的是絕對路徑中的父目錄。如果獲取的是相對路徑,返回null。
									//如果相對路徑中有上一層目錄那麼該目錄就是返回結果。



	}
	
	public static void method_3()throws IOException
	{
		File f = new File("d:\\java1223\\day20\\file2.txt");

		//f.createNewFile();

		//f.mkdir();


		//記住在判斷文件對象是否是文件或者目的時,必須要先判斷該文件對象封裝的內容是否存在。
		//通過exists判斷。
		sop("dir:"+f.isDirectory());
		sop("file:"+f.isFile());

		sop(f.isAbsolute());
	}


	public static void method_2()
	{
		File f = new File("file.txt");

		//sop("exists:"+f.exists());

		//sop("execute:"+f.canExecute());

		//創建文件夾
		File dir = new File("abc\\kkk\\a\\a\\dd\\ee\\qq\\aaa");

		sop("mkdir:"+dir.mkdirs());
	}
	

	public static void method_1()throws IOException
	{
		File f = new File("file.txt");
//		sop("create:"+f.createNewFile());
		//sop("delete:"+f.delete());



		
	}







	//創建File對象
	public static void consMethod()
	{
		//將a.txt封裝成file對象。可以將已有的和爲出現的文件或者文件夾封裝成對象。
		File f1 = new File("a.txt");

		//
		File f2 = new File("c:\\abc","b.txt");


		File d = new File("c:\\abc");
		File f3 = new File(d,"c.txt");

		sop("f1:"+f1);
		sop("f2:"+f2);
		sop("f3:"+f3);

		File f4 = new File("c:"+File.separator+"abc"+File.separator+"zzz"+File.separator+"a.txt");


	}

	public static void sop(Object obj)
	{
		System.out.println(obj);
	}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章