JavaSE基礎知識分享(十)

来源:https://www.cnblogs.com/cjybigdatablog/p/18366706
-Advertisement-
Play Games

寫在前面 今天繼續講JavaIO流的知識! Java 文件與流操作 File 類 File 類用於表示文件和文件夾的抽象。 構造方法 public File(String pathname): 使用路徑名創建 File 對象。 public File(String parent, String ch ...


寫在前面

今天繼續講JavaIO流的知識!

Java 文件與流操作

File 類

File 類用於表示文件和文件夾的抽象。

構造方法

  • public File(String pathname): 使用路徑名創建 File 對象。
  • public File(String parent, String child): 使用父目錄和子目錄名稱創建 File 對象。
  • public File(File parent, String child): 使用父 File 對象和子目錄名稱創建 File 對象。

成員方法

創建功能

  • public boolean createNewFile(): 創建一個新文件。
  • public boolean mkdir(): 創建單個目錄。
  • public boolean mkdirs(): 創建目錄,包括必要但不存在的父目錄。

刪除功能

  • public boolean delete(): 刪除文件或目錄。

重命名功能

  • public boolean renameTo(File dest): 重命名文件或目錄。

判斷功能

  • public boolean isDirectory(): 判斷是否為目錄。
  • public boolean isFile(): 判斷是否為文件。
  • public boolean exists(): 判斷文件或目錄是否存在。
  • public boolean canRead(): 判斷是否可讀。
  • public boolean canWrite(): 判斷是否可寫。
  • public boolean isHidden(): 判斷是否為隱藏文件。

基本獲取功能

  • public String getAbsolutePath(): 獲取絕對路徑。
  • public String getPath(): 獲取路徑。
  • public String getName(): 獲取文件名。
  • public long length(): 獲取文件長度。
  • public long lastModified(): 獲取最後修改時間。

高級獲取功能

  • public String[] list(): 獲取目錄中的文件和目錄名稱。
  • public File[] listFiles(): 獲取目錄中的 File 對象數組。

文件名稱過濾器

  • public String[] list(FilenameFilter filter): 使用文件名過濾器獲取文件和目錄名稱。
  • public File[] listFiles(FilenameFilter filter): 使用文件名過濾器獲取 File 對象數組。

位元組流

位元組輸入流

  • 抽象父類: InputStream
    • 實現子類:
      • FileInputStream
        • 創建對象的方式:
          FileInputStream fis = new FileInputStream("路徑");
          
        • 讀取數據的方式:
          • 一次讀取一個位元組:
            int i = 0;
            while ((i = fis.read()) != -1) {
                System.out.print((char) i);
            }
            
          • 一次讀取一個位元組數組:
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = fis.read(bytes)) != -1) {
                String s = new String(bytes, 0, length);
                System.out.print(s);
            }
            
          • 一次讀取一個位元組數組的一部分。
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = fis.read(bytes)) != -1) {
                String s = new String(bytes, 這裡寫從哪個索引開始截取, 這裡寫自己想要截取的長度);
                System.out.print(s);
            }
            
      • BufferedInputStream
        • 創建對象的方式:
          BufferedInputStream bis = new BufferedInputStream(new FileInputStream("路徑"));
          
        • 讀取數據的方式:
          • 一次讀取一個位元組:
            int i = 0;
            while ((i = bis.read()) != -1) {
                System.out.print((char) i);
            }
            
          • 一次讀取一個位元組數組:
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = bis.read(bytes)) != -1) {
                String s = new String(bytes, 0, length);
                System.out.print(s);
            }
            
          • 一次讀取一個位元組數組的一部分。
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = bis.read(bytes)) != -1) {
                String s = new String(bytes, 這裡寫從哪個索引開始截取, 這裡寫自己想要截取的長度);
                System.out.print(s);
            }
            

位元組輸出流

  • 抽象父類: OutputStream
    • 實現子類:
      • FileOutputStream

        • 創建對象的方式:
        #創建文件輸出流以寫入由指定的 File對象表示的文件。
        FileOutputStream(File file) 
        
        
        #創建文件輸出流以寫入由指定的 File對象表示的文件。  
        FileOutputStream(File file, boolean append) 
        
        
        #創建文件輸出流以指定的名稱寫入文件。  
        FileOutputStream(String name) 
        
        
        #創建文件輸出流以指定的名稱寫入文件。 
        FileOutputStream(String name, boolean append) 
        
        • 寫數據的方式:
          • 寫一個位元組:
            fos.write(i);
            
          • 寫一個位元組數組:
            byte[] bytes = {97, 98, 99};
            fos.write(bytes);
            
          • 寫位元組數組的一部分:
            byte[] bytes = {97, 98, 99};
            fos.write(bytes, 1, 2);
            
      • BufferedOutputStream

        • 創建對象的方式:
          BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("路徑"));
          
        • 寫數據的方式(註:這種方式寫文件在寫完之後需要flush一下,不然不會成功寫入,後面說的以file對象進行傳入參數的輸入流基本上都要寫完刷一下才有內容寫入):
          • 寫一個位元組:
            bos.write(i);
            
          • 寫一個位元組數組:
            byte[] bytes = {97, 98, 99};
            bos.write(bytes);
            
          • 寫位元組數組的一部分:
            byte[] bytes = {97, 98, 99};
            bos.write(bytes, 1, 2);
            

字元流

字元流是基於位元組流的,並加上了編碼表的支持。

字元輸入流

  • 抽象父類: Reader
    • 實現子類:
      • InputStreamReader
        • 創建對象的方式:
          InputStreamReader isr = new InputStreamReader(new FileInputStream("路徑"));
          
        • 讀取數據的方式:
          • 一次讀取一個字元:
            int i = 0;
            while ((i = isr.read()) != -1) {
                System.out.print((char) i);
            }
            
          • 一次讀取一個字元數組:
            char[] chars = new char[1024];
            int length = 0;
            while ((length = isr.read(chars)) != -1) {
                String s = new String(chars, 0, length);
                System.out.print(s);
            }
            
          • 一次讀取一個字元數組的一部分。
            char[] chars = new char[1024];
            int length = 0;
            while ((length = isr.read(chars)) != -1) {
                String s = new String(chars, 開始索引, 要讀的具體長度);
                System.out.print(s);
            }
            
      • FileReader
        • 創建對象的方式:
          FileReader fr = new FileReader("路徑");
          
        • 讀取數據的方式:
          • 一次讀取一個字元:
            int i = 0;
            while ((i = fr.read()) != -1) {
                System.out.print((char) i);
            }
            
          • 一次讀取一個字元數組:
            char[] chars = new char[1024];
            int length = 0;
            while ((length = fr.read(chars)) != -1) {
                String s = new String(chars, 0, length);
                System.out.print(s);
            }
            
          • 一次讀取一個字元數組的一部分。
            char[] chars = new char[1024];
            int length = 0;
            while ((length = fr.read(chars)) != -1) {
                String s = new String(chars, 開始索引, 要讀的具體長度);
                System.out.print(s);
            }
            
      • BufferedReader
        • 創建對象的方式:
          • 方式1:
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("路徑")));
            
          • 方式2(簡化版):
            BufferedReader br = new BufferedReader(new FileReader("路徑"));
            
        • 讀取數據的方式:
          • 一次讀取一個字元:
            int i = 0;
            while ((i = br.read()) != -1) {
                System.out.print((char) i);
            }
            
          • 一次讀取一個字元數組:
            char[] chars = new char[1024];
            int length = 0;
            while ((length = br.read(chars)) != -1) {
                String s = new String(chars, 0, length);
                System.out.print(s);
            }
            
          • 一次讀取一個字元數組的一部分。
            char[] chars = new char[1024];
            int length = 0;
            while ((length = br.read(chars)) != -1) {
                String s = new String(chars, 開始索引, 具體長度);
                System.out.print(s);
            }
            
          • 一次讀取一行數據(不包括換行符):
            String line = null;
            while ((line = br.readLine()) != null) {
                System.out.print(line);
            }
            

字元輸出流

  • 抽象父類: Writer
    • 實現子類:
      • OutputStreamWriter

        • 創建對象的方式:
          OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("路徑"));
          
        • 寫數據的方式:
        • 一次寫一個字元。
          osw.write(i);
          
        • 一次寫一個字元數組。
          char[] chars = {'a', 'b', 'c'};
          osw.write(chars);
          
        • 一次寫一個字元數組的一部分。
          char[] chars = {'a', 'b', 'c'};
          osw.write(chars, 1, 2);
          
        • 一次寫一個字元串。
          osw.write("example");
          
        • 一次寫一個字元串的一部分。
          osw.write("example", 1, 4);
          
      • FileWriter

        • 創建對象的方式:
          FileWriter fw = new FileWriter("路徑");
          
        • 寫數據的方式:
          • 一次寫一個字元。
            fw.write(i);
            
          • 一次寫一個字元數組。
            char[] chars = {'a', 'b', 'c'};
            fw.write(chars);
            
          • 一次寫一個字元數組的一部分。
            char[] chars = {'a', 'b', 'c'};
            fw.write(chars, 1, 2);
            
          • 一次寫一個字元串。
            fw.write("example");
            
          • 一次寫一個字元串的一部分。
            fw.write("example", 1, 4);
            
      • BufferedWriter

        • 創建對象的方式:
          • 方式1:
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("路徑")));
            
          • 方式2(簡化版):
            BufferedWriter bw = new BufferedWriter(new FileWriter("路徑"));
            
        • 寫數據的方式:
          • 一次寫一個字元:
            bw.write(i);
            
          • 一次寫一個字元數組:
            char[] chars = {'a', 'b', 'c'};
            bw.write(chars);
            
          • 一次寫一個字元數組的一部分:
            char[] chars = {'a', 'b', 'c'};
            bw.write(chars, 1, 2);
            
          • 一次寫一個字元串:
            bw.write("example");
            
          • 一次寫一個字元串的一部分:
            bw.write("example", 1, 4);
            
          • 特殊功能:自動生成換行符:
            bw.newLine();
            

文件的讀寫複製

位元組流

  • 普通位元組輸入輸出流:一次讀寫一個位元組。
    FileInputStream inputPath = new FileInputStream("inputpath");
    FileOutputStream outputPath = new FileOutputStream("outpath");

    int i = 0;
    while ((i = inputPath.read()) != -1) {
        outputPath.write((char) i);
     }
    inputPath.close();
    outputPath.close();
  • 帶緩衝的位元組輸入輸出流:一次讀寫一個位元組。
    BufferedInputStream inputpath = new BufferedInputStream(new FileInputStream("inputpath"));
    BufferedOutputStream outputpath = new BufferedOutputStream(new FileOutputStream("outpath"));

    int i = 0;

    while ((i = inputpath.read()) != -1) {
        outputpath.write((char) i);
        outputpath.flush;
     }

    inputpath.close();
    outputpath.close();
        
  • 普通位元組輸入輸出流:一次讀寫一個位元組數組。
    FileInputStream inputPath = new FileInputStream("inputpath");
    FileOutputStream outputPath = new FileOutputStream("outpath");

    byte[] bytes = new byte[1024];
    int length = 0;
    while ((length = inputPath.read(bytes)) != -1) {
        outputPath.write(bytes, 0, length);
        outputPath.flush();
     }
    inputPath.close();
    outputPath.close();
  • 帶緩衝的位元組輸入輸出流:一次讀寫一個位元組數組(此方式的效率最高)。
    BufferedInputStream inputpath = new BufferedInputStream(new FileInputStream("inputpath"));
    BufferedOutputStream outputpath = new BufferedOutputStream(new FileOutputStream("outpath"));

    byte[] bytes = new byte[1024];
    int length = 0;
    while ((length = inputpath.read(bytes)) != -1) {
        outputpath.write(bytes, 0, length);
        outputpath.flush();
    }

    inputpath.close();
    outputpath.close();

字元流

  • 普通字元輸入輸出流:一次讀寫一個字元。
    InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("inputpath"));
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("outputpath"));

    int i = 0;
    while ((i = inputStreamReader.read()) != -1) {
        outputStreamWriter.write(i);
        outputStreamWriter.flush();
    }

    inputStreamReader.close();
    outputStreamWriter.close();
  • 普通字元輸入輸出流:一次讀寫一個字元數組。
    InputStreamReader inputStreamReader = new InputStreamReader(new FileInputStream("inputpath"));
    OutputStreamWriter outputStreamWriter = new OutputStreamWriter(new FileOutputStream("outputpath"));

    char[] chars = new char[1024];
    int length = 0;
    while ((length = inputStreamReader.read(chars)) != -1) {
        outputStreamWriter.write(chars,0,length);
        outputStreamWriter.flush();
    }

    inputStreamReader.close();
    outputStreamWriter.close();
  • 簡化寫法字元輸入輸出流:一次讀寫一個字元。
    FileReader fileReader = new FileReader("inputpath");
    FileWriter fileWriter = new FileWriter("outputpath");

    int i = 0;
    while ((i = fileReader.read()) != -1) {
        fileWriter.write(i);
        fileWriter.flush();
    }

    fileWriter.close();
    fileReader.close();
  • 簡化寫法字元輸入輸出流:一次讀寫一個字元數組。
    FileReader fileReader = new FileReader("inputpath");
    FileWriter fileWriter = new FileWriter("src/com/shujia/data/b.txt");

    char[] chars = new char[1024];
    int length = 0;
    while ((length = fileReader.read(chars)) != -1) {
        fileWriter.write(chars,0,length);
        fileWriter.flush();
    }

    fileWriter.close();
    fileReader.close();
  • 字元緩衝輸入輸出流:一次讀寫一個字元。
    BufferedReader bufferedReader = new BufferedReader(new FileReader("inputpath"));
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("outpath"));

    int i = 0;
    while ((i = bufferedReader.read()) != -1) {
         bufferedWriter.write(i);
         bufferedWriter.flush();
    }

    bufferedWriter.close();
    bufferedReader.close();
  • 字元緩衝輸入輸出流:一次讀寫一個字元數組。
    BufferedReader bufferedReader = new BufferedReader(new FileReader("inputpath"));
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("outpath"));

    char[] chars = new char[1024];
    int length = 0;
    while ((length = bufferedReader.read(chars)) != -1) {
    bufferedWriter.write(chars,0,length);
    bufferedWriter.flush();
    }

    bufferedWriter.close();
    bufferedReader.close();
  • 字元緩衝輸入輸出流:一次讀寫一行數據。
    BufferedReader bufferedReader = new BufferedReader(new FileReader("inputpath"));
    BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("outpath"));

    String i = null;
    while ((i = bufferedReader.readLine()) != null) {
        bufferedWriter.write(i);
        bufferedWriter.newLine();
        bufferedWriter.flush();
     }

    bufferedWriter.close();
    bufferedReader.close();

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • title: 使用 navigateTo 實現靈活的路由導航 date: 2024/8/13 updated: 2024/8/13 author: cmdragon excerpt: 摘要:本文詳細介紹 Nuxt.js 中的 navigateTo 函數,包括基本用法、在路由中間件中使用、導航到外部 ...
  • SCS(- )架構介紹SCS(- )架構定義與特性自主Web應用:SCS是一個自主的Web應用程式,包含Web UI、邏輯和持久化層。這種完整性確保了一個用戶故事通常可以通過修改一個SCS來實現。 API的可選性:SCS可以有一個服務API,但這並非必需。如果SCS內的邏輯僅由SCS內的UI使用,則 ...
  • 分散式事務的兩階段提交(2PC)和三階段提交(3PC)是分散式系統中常用的事務管理協議,它們各自有優缺點。 兩階段提交(2PC) 流程: 準備階段(Prepare Phase): 協調者向所有參與者發送請求,詢問是否可以提交事務。 每個參與者執行本地事務操作,但不提交(僅預提交),並返回成功或失敗的 ...
  • 簡介 發佈訂閱模式是一種常用的用於解耦的模式。 它和觀察者模式的區別在於: 觀察者模式:被觀察者需要維護一個觀察者的集合; 發佈訂閱模式:通信雙方互相不知道對方的存在,通過第三方事件匯流排進行通信。 發佈訂閱模式在前端領域很常見,例如: Vue 框架中組件的$on和$emit方法; Node.js 中 ...
  • 如果你是一名業務開發,你可能要說,我整天就是做CRUD(增刪改查),哪裡需要瞭解什麼應用架構設計? 經常有人說,程式員 35 歲之後很容易陷入瓶頸,被行業淘汰,我覺得原因其實就在此。 有些朋友在寫代碼的時候,可能沒有太多考慮非功能性的需求、擴展性,只是完成功能,覺得能用就好。做事情的時候,也沒有長遠 ...
  • 題目描述 給定一個只包括 '(',')','{','}','[',']' 的字元串 s ,判斷字元串是否有效。 有效字元串需滿足: 左括弧必須用相同類型的右括弧閉合。 左括弧必須以正確的順序閉合。 每個右括弧都有一個對應的相同類型的左括弧。 大體思路 這裡我們使用棧這個數據結構來解決,我們從左到右依 ...
  • Python Lambda 用法大全 一、Lambda表達式基礎 Lambda 的組成分為三部分 Lambda表達式是Python中的一種匿名函數,它可以在一行代碼中定義函數並立即調用它。與普通函數不同的是,Lambda函數通常不需要使用def關鍵字去定義,而是在需要時直接使用lambda關鍵字來創 ...
  • 在PHP 7.4中安裝xdebug,出現"configure: error: rtnetlink.h is required, please make sure it is available by installing the correct package"的問題。 在使用sudo yum in ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...