1. 程式人生 > 其它 >Java基礎-IO流-java.io.File

Java基礎-IO流-java.io.File

技術標籤:# 知識樹/Java基礎 /IO流

Java工程師知識樹 / Java基礎


文章目錄


File描述

java.io.File:是檔案和目錄路徑名的抽象表示,主要用於檔案和目錄建立、查詢和刪除等操作。

java.io.File類專門對檔案進行操作的類,只能對檔案本身進行操作,不能對檔案內容進行操作。

明確絕對路徑與相對路徑的概念:

絕對路徑是指檔案在硬碟上真正存在的路徑

相對路徑相對於自己的目標檔案位置程式中使用時,相對路徑則表示相對於當前的專案目錄的路徑

java.io.File內常用欄位摘要
欄位描述
static char separatorChar與系統有關的預設名稱分隔符。
static String separator與系統有關的預設名稱分隔符,為了方便,它被表示為一個字串。
System.out.println("E:\\document\\io"+File.separatorChar+"a.txt");//拼接目錄與檔案所用
System.out.println("E:\\document\\io"+File.separator+"a.txt");//拼接目錄與檔案所用
System.out.println
("E:\\document\\io\\a.txt");

構造方法

構造方法摘要構造方法描述例項
File(File parent, String child)根據 parent 抽象路徑名和 child 路徑名字串建立一個新 File 例項。File parentDir = new File(“E:\document\io”);//父級File物件也就是檔案所在目錄物件
String child = “a.txt”;// 子路徑字串 也就是檔名加字尾
File(String pathname)通過將給定路徑名字串轉換為抽象路徑名來建立一個新 File 例項。String path = “E:\document\io\a.txt”; // 檔案路徑名
File(String parent, String child)根據 parent 路徑名字串和 child 路徑名字串建立一個新 File 例項。String parent = “E:\document\io”;// 通過父路徑也就檔案所在目錄
String child = “a.txt”;// 子路徑字串 也就是檔名加字尾
File(URI uri)通過將給定的 file: URI 轉換為一個抽象路徑名來建立一個新的 File 例項。URI uri = URI.create(“file:/E:/document/io/b.txt”);//URI物件

File獲取與判斷功能的方法

列印測試方法

import java.io.File;
import java.net.URI;
import java.text.DateFormat;
import java.util.Date;

public class FileTest extends File {
    public FileTest(String pathname) {
        super(pathname);
    }

    public FileTest(String parent, String child) {
        super(parent, child);
    }

    public FileTest(File parent, String child) {
        super(parent, child);
    }

    public FileTest(URI uri) {
        super(uri);
    }

    @Override
    public String toString() {
        StringBuffer str = new StringBuffer();
        str.append("-----------"+super.toString()+"---------------");

        // File獲取方法
        str.append("\r\n");
        str.append("File的名稱-getName:");
        str.append(super.getName());
        str.append("\r\n");
        str.append("File的絕對路徑名-getAbsolutePath:");
        str.append(super.getAbsolutePath());
        str.append("\r\n");
        /**
         * getPath () 與 getAbsolutePath () 的區別在於,
         * 前者獲取的是構造 File 例項時的路徑,後者獲取的是 File 例項的絕對路徑。
         * 當構造 File 例項的路徑也是絕對路徑時,二者是一樣的。
         */
        str.append("File的路徑名字串-getPath:");
        str.append(super.getPath());
        str.append("\r\n");
        str.append("File的路徑名字串-toString:");
        str.append(super.toString());// toString() 呼叫 return getPath(); 等效於getPath()
        str.append("\r\n");
        str.append("File的長度-length:");
        str.append(super.length());
        str.append("\r\n");
        str.append("File的最後一次被修改的時間-lastModified:");
        str.append(DateFormat.getInstance().format(new Date(super.lastModified())));
        str.append("\r\n");
        str.append("抽象路徑名父目錄的路徑名字串;如果此路徑名沒有指定父目錄,則返回 null-getParent:");
        str.append(super.getParent());

        // File判斷方法
        str.append("\r\n");
        str.append("File是否存在-exists:");
        str.append(super.exists());
        str.append("\r\n");
        str.append("File是否是一個檔案-isFile:");
        str.append(super.isFile());
        str.append("\r\n");
        str.append("File是否是一個隱藏檔案-isHidden:");
        str.append(super.isHidden());
        str.append("\r\n");
        str.append("File是否是一個目錄-isDirectory:");
        str.append(super.isDirectory());
        str.append("\r\n");
        str.append("應用程式是否可以讀取此File-canRead:");
        str.append(super.canRead());
        str.append("\r\n");
        str.append("應用程式是否可以修改此File-canWrite:");
        str.append(super.canWrite());
        return str.toString();
    }
}

測試功能程式碼

import java.net.URI;

public class TestIO {

    public static void main(String[] args) {

		//存在的檔案
        FileTest file1 = new FileTest("E:\\document\\io\\a.txt");
        System.out.println(file1);
        //存在的目錄
        FileTest file2 = new FileTest("E:\\document\\io");
        System.out.println(file2);
        //不存在的檔案
        FileTest file3 = new FileTest("E:\\document\\io\\c.txt");
        System.out.println(file3);
        //存在的檔案通過URI方法建立
        URI uri = URI.create("file:/E:/document/io/b.txt");
        FileTest file4 = new FileTest(uri);
        System.out.println(file4);

    }

}

E:\\document\\io目錄下的情況為:

建立與刪除方法

  • public boolean createNewFile() :指定 File 例項的檔案不存在時,建立空檔案
  • public boolean delete() :刪除指定 File 例項表示的檔案或目錄
  • public boolean mkdir() :建立指定 File 例項表示的目錄
  • public boolean mkdirs() :建立指定 File 例項表示的目錄,以及父目錄
// 檔案的建立
File createNewFileTest = new File("E:\\document\\io\\testCreateNewFile.txt");
System.out.println("是否存在:"+createNewFileTest.exists()); // false
System.out.println("是否建立:"+createNewFileTest.createNewFile()); // true
System.out.println("是否建立:"+createNewFileTest.createNewFile()); // 以及建立過了所以再使用createNewFile返回false
System.out.println("是否存在:"+createNewFileTest.exists()); // true
// 目錄的建立
File mkdirTest= new File("newDirectory");
System.out.println("是否存在:"+mkdirTest.exists());// false
System.out.println("是否建立:"+mkdirTest.mkdir()); // true
System.out.println("是否存在:"+mkdirTest.exists());// true
// 建立多級目錄
File mkdirsTest= new File("E:\\document\\io\\newDirectoryOne\\newDirectoryTwo");
System.out.println(mkdirsTest.mkdirs());// true
// 檔案的刪除
System.out.println(createNewFileTest.delete());// true
// 目錄的刪除
System.out.println(mkdirsTest.delete());// true

遍歷目錄下檔案列表

  • public String[] list() :返回一個 String 陣列,表示指定 File 例項目錄中的所有子檔案或目錄。值為File的getName()集合
  • public File[] listFiles() :返回一個 File 陣列,表示指定 File 例項目錄中的所有的子檔案或目錄。

測試程式碼:

File directory = new File("E:\\document\\io");
//獲取當前目錄下的檔案以及資料夾的名稱。File的getName()集合
String[] names = directory.list();
for(String name : names){
    System.out.println(name);
}
//獲取當前目錄下的檔案以及資料夾物件
File[] files = directory.listFiles();
for (File file : files) {
    System.out.println(file);
}
列印:
a.txt
b.txt
File.md
newDirectoryOne
testCreateNewFile.txt
E:\document\io\a.txt
E:\document\io\b.txt
E:\document\io\File.md
E:\document\io\newDirectoryOne
E:\document\io\testCreateNewFile.txt