1. 程式人生 > >黑馬程式設計師——IO流-File類

黑馬程式設計師——IO流-File類

------- android培訓java培訓、期待與您交流! ----------

File

1.      File類封裝了一個路徑,可以資料夾路徑, 檔案路徑或是一個不存在的路徑

2.      IO流通過File類可以操作資料夾

構造方法

1.      File(File parent, String child)

這個構造方法可以構造一個有已有的File路徑和child拼接成的新路徑

例如: File file = new File(“d:”); File file1 = new File(file, “myforder”),拼接成的新路徑是d:\myforder

2.      File(String parent, Stringchild)

這個構造方法可以構造由字串parentchild構建的新路徑

3.      File(String pathname)

這個構造方法構造一個指定的字串表示的路徑

常用方法

1.      建立資料夾

1)      pubic boolean mkdir()

建立一個物件指定路徑的資料夾,一次只能建立一個子目錄

例如: File file = new File(“c:”); File file1 = new File(file, “a”);這個是可以的

            File file= new File(“c:”); File file1 = new File(file, “a/b”);

這個就不可以

下面的方法能建立多層路徑

2)      public boolean mkdirs()

建立一個物件指定目錄的資料夾,可以建立多層目錄

2.      建立檔案

public boolean createFile()

建立一個物件指定路徑的檔案, 前提是這個路徑必須是有效的路徑

3.      是否存在

public boolean isExists()

判斷當前物件指定的路徑是否存在

4.      是否為檔案

public boolean isFile()

判斷當前物件指定的路徑是否為檔案

5.      是否為資料夾

public boolean isDirectory()

判斷當前物件指定的路徑是否為資料夾

6.      是否是絕對路徑

public boolean isAbsolute

判斷當前物件指定的是否為絕對路徑值

絕對路徑就是加上碟符的一整個檔案路徑

7.      獲得絕對路徑

1)      public File getAbsoluteFile()

獲得當前物件指定路徑的絕對路徑,返回File

2)      public String getAbsolutePath()

返回當前物件指定路徑的絕對路徑的字串形式

8.      獲取父級路徑

1)      public File getParentFile()

返回當前物件的指定檔案的上一級路徑,返回File, 沒有上一級目錄返回null

2)      public String getParent()

返回上一級路徑的字串形式, 沒有返回null

9.      獲取檔名

public StringgetName()

返回當前物件指定路徑的檔案或者資料夾的名字

10.  public File[] listFile()

獲得當前指定路徑下的所有子檔案,返回一個File[]

可以通過迭代得到每一個子檔案

11.  public boolean renameTo(Filefile)

對當前物件指定路徑的路徑和檔名重新定義,就像是windows系統下的剪下

12.  public boolean delete()

這個方法刪除當前指定路徑的空資料夾或者檔案

13.  獲得硬碟大小

1)      public long getTotalSpace()

獲得當前碟符總共的大小, 單位是位元組

2)      public long getFreeSpace()

獲得當前碟符空閒的位元組大小

3)      pubic long getUsedSpace()

獲得當前碟符下的可用空間的位元組大小

14.  檔案修改時間

1)      public long lastModified()

獲取當前物件指定路徑表示的檔案最後修改的時間,返回一共經過的毫秒值

2)      public long setModified()

設定當前物件指定路徑表示的檔案最後修改的時間

遞迴

1.      遞迴就是函式自己呼叫自己,在處理一些問題時, 可以會重複某些步驟,這時就可以用遞迴解決.

例如:獲取資料夾中所有子檔案, 刪除資料夾,統計資料夾大小, 拷貝資料夾

2.      遞迴時注意事項

1)      遞迴要有退出條件.

2)      遞迴效率不高,比較消耗記憶體.

3)      如果需要修改預設記憶體大小,使用-Xss引數

BigInteger, BigDecimal

這兩各類用於儲存超出long範圍的整數和超出double範圍的浮點數

這兩個類提供了加減乘除和取模運算,在運算時不能直接用運算子操作

例如: 9223372036854775807L + 1BigIteger中算加法是

BigInteger sum = BigInteger(“9223372036854775807”).add(“1”);

         BigInteger類提供運算的方法是

         publicBigInteger add(BigInteger i)                  加法

         publicBigInteger substract(BigInteger i)                 減法

         publicBigInteger multiply(BigInteger i)          乘法

         publicBigInteger divide(BigInteger i)              除法

         publicBigInteger mod(BigInteger i)                 取模

關於BigDecimal中的方法與BigInteger基本一致

File類程式碼示例

這段程式碼中分別定義了檔案以及資料夾大小統計,刪除檔案及資料夾, 拷貝檔案和資料夾的方法

package cn.itcast.io.file.exercise;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class MyOperator {

	/**
	 * @param args
	 * @throws IOException
	 * @throws FileNotFoundException
	 */
	public static void main(String[] args) throws FileNotFoundException,
			IOException {

		File src = new File("e:/學習軟體");
		File dest = new File("d:");
		myCopy(src, dest);
		System.out.println("拷貝成功");
		// File del = new File("d:/a.txt");
		// myDelete(del);
		// File file = new File("d:/Java語言程式設計-基礎篇(原書第8版).pdf");
		// System.out.println(mySizeCount(file));
	}

	// 統計檔案或者資料夾大小的方法
	private static long mySizeCount(File file) {
		// 變數用來統計總檔案大小
		long sum = 0;

		// 如果當前是一個檔案, 直接返回長度, 如果是資料夾, 迭代統計
		if (file.isFile()) {
			sum += file.length();
		} else {
			// 得到傳入File物件的子目錄的File陣列
			File[] files = file.listFiles();
			// 如果當前目錄不為空, 迭代統計檔案大小
			if (files != null) {
				for (File subFile : files) {
					// 如果迭代器指向檔案, 那麼將檔案大小累計
					if (subFile.isFile())
						sum += subFile.length();
					// 如果迭代器指向資料夾, 那麼將遞迴得到每個檔案的大小累計
					if (subFile.isDirectory())
						sum += mySizeCount(subFile);
				}
			}
		}
		// 返回累計結果
		return sum;
	}

	// 刪除檔案或者資料夾的方法
	private static void myDelete(File file) {
		// 如果當前file物件時一個檔案, 直接刪除
		if (file.isFile()) {
			file.delete();
		} else {
			// 如果當前file是一個資料夾, 那麼迭代刪除
			File[] files = file.listFiles();
			if (files != null) { // 可能有些資料夾下有內容, 但是不能被操作
				for (File subFile : files) {
					// 如果是檔案, 那麼刪除
					if (subFile.isFile())
						subFile.delete();
					// 如果是資料夾, 那麼迭代
					if (subFile.isDirectory())
						myDelete(subFile);
				}
			}
			// 刪除空資料夾
			file.delete();
			System.out.println("刪除成功");
		}
	}

	// 拷貝檔案或者資料夾的方法
	private static void myCopy(File src, File dest)
			throws FileNotFoundException, IOException {
		// 如果要copy的是一個檔案, 那麼直接copy
		if (src.isFile()) {
			try (
			// 建立兩個流物件用於讀取和寫入
			BufferedInputStream bis = new BufferedInputStream(
					new FileInputStream(src));
					BufferedOutputStream bos = new BufferedOutputStream(
							new FileOutputStream(new File(dest, src.getName())));

			) {
				int x;
				while ((x = bis.read()) != -1) {
					bos.write(x);
				}
			}
		} else { // 如果是資料夾, 那麼就要遍歷拷貝
			// 在目標目錄建立一個跟原始檔一樣的目錄
			File newDest = new File(dest, src.getName());
			newDest.mkdir();
			// 得到原始檔目錄的全部子檔案
			File[] files = src.listFiles();
			// 如果原始檔目錄的檔案可以操作, 那麼迭代操作
			if (files != null) {
				for (File subFile : files) {
					// 如果當前子檔案是資料檔案, 那麼拷貝
					if (subFile.isFile()) {
						try (
						// 建立兩個流物件用於讀取和寫入
						BufferedInputStream bis = new BufferedInputStream(
								new FileInputStream(subFile));
								BufferedOutputStream bos = new BufferedOutputStream(
										new FileOutputStream(new File(newDest,
												subFile.getName())));) {
							int x;
							while ((x = bis.read()) != -1)
								bos.write(x);
						}
					}
					// 如果是目錄, 那麼遞迴拷貝
					if (subFile.isDirectory()) {
						myCopy(subFile, newDest);
					}
				}
			}
		}
	}
}