文件

<small>

文件 目錄(信息)

文件目錄信息類

new File(String pathName)

此構造絕對不是創建文件,僅僅表示根據抽象路徑pathName,創建一個File文件實例
File實例,并不能說明文件就真實存在,只是用來判斷文件目錄是否真實存在

boolean createNewFile()

創建文件并返回是否創建成功

boolean delete()

刪除文件

boolean isFile()

判斷是否為文件

boolean isDirectory()

判斷是否為目錄
程序實例:

String pathName = "E:\\ksxx\\所有知識點講解\\21\\demo.txt";
        //創建一個File實例
        File file = new File(pathName);
        boolean exists()
         */
        boolean isExists = file.exists();
        System.out.println("文件是否存在:"+isExists);
        if(!isExists){
            boolean isCreateFileOk = 
                    file.createNewFile();
            System.out.println(isCreateFileOk?"創建文件成功":"創建文件失敗");
        }else{
            boolean isDeleteFileOk = file.delete();
            System.out.println(isDeleteFileOk?"刪除文件成功":"刪除文件失敗");
        }
pathName = "E:\\";
        file = new File(pathName);
        System.out.println("E:\\存在:"+file.exists());
boolean isFile = file.isFile();
        System.out.println(isFile?"E:\\是文件":"E:\\不是文件");
boolean isDirectory = file.isDirectory();
        System.out.println(isDirectory?"E:\\是目錄":"E:\\不是目錄");
file = new File("/");
        System.out.println(file.isDirectory()?"/是目錄":"/不是目錄");
pathName = "E:"+File.separator+"ksxx"+File.separator+
                    "所有知識點講解"+File.separator+
                    "21"+File.separator+"demo.txt";
        System.out.println(pathName);
pathName = "../demo02.txt";
        file = new File(pathName);
        file.createNewFile();//創建當前目錄下的demo01.t

1、windows與linux的區別:
* widows中,目錄的表示為
* 沒有根目錄,但有盤符,
* 如:C:\、D:\、E:\、F:\、U盤等
* linux中,目錄的表示為 /
* 沒有盤符概念,但有且只有一個根目錄。
* 所有的路徑都是從根目錄出發。
* U盤被視為掛載點。
*
* 不管是什么系統的服務器,
* 可以利用File.separator來實現目錄的自動辨析。
* 當遇到是windows系統服務器,就會自動填充
* 當遇到是linux系統服務器,就會自動填充 /

/*
* 2、/、./與../的區別
* 如果后面不跟內容,點后面的/可以省略(建議不要省略)
*
* / 根路徑
* ./(可以直接省略) 用來表示當前目錄下
* [./]../ 用來表示上級目錄
*
* 絕對路徑:
* 以 / 開頭的路徑。
*
* 相對路徑:
* 以./貨../ 開頭的路徑。

long length()

獲得文件的大小(字節量)

lastModified()

獲得文件最后一次修改時間

public class FileDemo02 {
    public static void main(String[] args) throws IOException {
        /*
         * File:文件目錄信息類
         *   查詢文件信息方法
         */
        String pathName = 
                "E:\\ksxx\\所有知識點講解\\21\\demo.txt";
        //創建一個File實例
        File file = new File(pathName);
        /*
         * 6、獲得文件的大小(字節量)
         *    long length()
         */
        long bytes = file.length();
        System.out.println("demo.txt文件大小:"+bytes);
        
        /*
         * 7、獲得文件的最后一次修改時間
         */
        long lastModifiedTime = file.lastModified();
        System.out.println("demo01.txt最后一次修改時間:"+new Date(lastModifiedTime));
        
        System.out.println("是否為可讀文件:"+file.canRead());
        System.out.println("是否為可寫文件:"+file.canWrite());
        System.out.println("是否為可執行文件:"+file.canExecute());
        System.out.println("是否為隱藏文件:"+file.isHidden());
        
        System.out.println("獲得文件名:"+file.getName());
        System.out.println("獲得目錄名:"+file.getParent());
        System.out.println("獲得抽象路徑:"+file.getPath());
boolean midir();

只能創建單級目錄,只有上級目錄存在的前提下,才能創建單級目錄。如果多級目錄不存在,則無法創建。

boolean mkdirs

可以創建多級目錄

RandomAcessFile

文件讀寫操作類:可以通過File實例對文件實現讀寫操作。
RandomAccessFile的構造及注意事項
* RandomAccessFile(String name,String mode)
* RandomAccessFile(File file,String mode)
*
* 構造二,其實是對構造一的封裝。
* RandomAccessFile(new File(name),String mode)
*
* 構造參數:
* 第一個參數:字符串類型的抽象路徑
* 也可以是抽象路徑封裝好的File實例。
*
* 第二個參數:讀寫模式
* 讀:r (read)
* 寫:w (write)
*
* RandomAccessFile對象創建的
* 注意事項:
* 1、文件路徑(文件所在的目錄)非真實存在,
* 會拋出異常:FileNotFoundException
* 2、文件路徑(文件所在的目錄)真實存在,
* 只不過是文件不存在:
* ①、只讀模式,必須要求文件存在,
* 否則拋出FileNotFoundException
* ②、讀寫模式,文件不存在,
* 會自動創建文件。

RandomAccessFile(String name,String mode)
RandomAccessFile(File file,String mode)
String pathName = "./demo01.txt";
        RandomAccessFile raf = 
                new RandomAccessFile(pathName,"rw");
        //或者
        File file = new File(pathName);
        raf = new RandomAccessFile(file,"rw");

文件的讀寫

單個字節的讀寫
String pathName = "./demo01.txt";
        RandomAccessFile raf = 
                new RandomAccessFile(pathName,"rw");
raf.write('a');//a
        raf.write(353);//其實寫進去還是a
        raf.write(865);//其實寫進去還是a
int read1 = raf.read();
//      System.out.println(read1);//-1表示讀取到文件末尾
raf.close();//讀寫完畢后,需要關閉io通道在一定意義上,就是Ctrl+S保存
raf = new RandomAccessFile(pathName,"rw");
        int read1 = raf.read();
        System.out.println(read1);//97
        
        int read2 = raf.read();
        System.out.println(read2);//97
        
        int read3 = raf.read();
        System.out.println(read3);//97
        
        int read4 = raf.read();
        System.out.println(read4);//-1
        
        raf.close();

文件的復制

單字節復制
RandomAccessFile file = 
                new RandomAccessFile("16.rar","rw");
        RandomAccessFile copyFile = 
                new RandomAccessFile("16_copy.rar","rw");
        
        long startTime = System.currentTimeMillis();
        /*
         * 要對文件實現單字節的讀寫操作,
         *  必須使用循環來完成。
         */
        int read = 0;
        //對file進行單字節讀取操作
        while((read = file.read()) != -1){
            //讀一個字節
            //對copyFile進行單字節寫操作
            copyFile.write(read);//寫
        }
        
        //讀寫完畢,要關流
        file.close();
        copyFile.close();
        long endTime = System.currentTimeMillis();
        //969128ms
        System.out.println("本次拷貝,用時:"+(endTime - startTime)+"ms");
字節數組的復制
public static void main(String[] args) throws IOException {
        /*
         * 通過RandomAccessFile批量字節的讀寫操作
         *      來完成對文件16.rar的拷貝。
         *      目標文件為16_copy.rar
         *      
         *      實現步驟:
         *          先對16.rar文件進行批量字節讀取。
         *          讀取的字節,
         *          向16_copy.rar中寫。
         */
        RandomAccessFile file = 
                new RandomAccessFile("16.rar","rw");
        RandomAccessFile copyFile = 
                new RandomAccessFile("16_copy2.rar","rw");
        
        long startTime = System.currentTimeMillis();
        /*
         * 要對文件實現單字節的讀寫操作,
         *  必須使用循環來完成。
         */
        int read = 0;
        byte[] buf = new byte[1024];
        //對file進行字節數組讀取操作
        while((read = file.read(buf)) != -1){
            //讀字節數組
            //對copyFile進行字節數組寫操作
            copyFile.write(buf);//寫
        }
        
        //讀寫完畢,要關流
        file.close();
        copyFile.close();
        long endTime = System.currentTimeMillis();
        //1158ms
        System.out.println("本次拷貝,用時:"+(endTime - startTime)+"ms");

字節數組的復制效率高很多

總結:
file只是文件目錄的信息類,根據抽象路徑,創建file實例,通file實例,可以查詢文件,目錄的相關信息
file不代表文件本身,并不能對文件實現讀寫操作

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容