首頁  >  文章  >  Java  >  Java 中的檔案處理

Java 中的檔案處理

WBOY
WBOY原創
2024-08-30 15:36:53893瀏覽

檔案處理是指在java中處理文件。讀取檔案和寫入 java 檔案稱為 java 中的檔案處理。 FILE 是一個可以包含不同類型資訊的容器。文件可以包含文字、圖像、影片、表格等

廣告 該類別中的熱門課程 JAVA 掌握 - 專業化 | 78 課程系列 | 15 次模擬測驗

開始您的免費軟體開發課程

網頁開發、程式語言、軟體測試及其他

在 java 中,File 類別使我們能夠處理不同類型的檔案。  File 類別是 java.io 套件的成員。 Java提供了各種讀取、寫入、更新和刪除檔案的方法。

操作類型

可以對檔案執行的不同類型的操作如下:

Java 中的檔案處理

  • 建立檔案
  • 更新檔案
  • 刪除檔案
  • 上傳檔案到特定位置
  • 開啟檔案
  • 關閉檔案

文法:

要在程式中使用文件,您需要匯入 java.io 套件。導入此套件將為您提供一個 File 類,您可以透過在 File 類別的建構函數中引用該檔案來初始化該類別。

//importing file class
import java.io.File;
//File name passed to the object
File fileObj = new File("file.txt");

文件處理如何運作?

在 Java 中,檔案處理是透過流概念進行的。文件的輸入/輸出操作透過流執行。流是指資料的序列。

在java中,Stream有兩種:

Java 中的檔案處理

  • 字符流:字符流是指涉及字符的流。 資料處理是透過文件中的字元流進行的。
  • 位元組流:我們將以位元組為單位傳輸資料的流稱為位元組流。資料處理透過檔案中的位元組流進行。

檔案處理方法

下面給了一些在 java 中執行不同操作的方法:

  1. createNewFile():createNewFile 方法用來建立一個空檔案。它以布林值形式傳回響應。
  2. getName(): 此方法用於取得檔案名稱。它傳回字串,即回應中的檔案名稱。
  3. getAbsolutePath(): 它傳回檔案的絕對路徑。該方法的傳回類型是字串。
  4. canRead(): 此方法檢查檔案是否可讀。 它回傳一個布林值。
  5. canWrite(): 此方法檢查檔案是否可寫入。它傳回一個布林值。
  6. delete(): 此方法用於刪除檔案。它傳回一個布林值。
  7. exists(): 使用此方法檢查檔案是否存在。 它回傳一個布林值。
  8. length(): 此方法傳回檔案大小(以位元組為單位)。此方法的傳回類型為long。
  9. list(): 此方法傳回目錄中可用檔案的陣列。它傳回一個字串值數組。
  10. mkdir(): 若要建立新目錄,請使用此方法。它傳回一個布林值。

Java 檔案處理範例

以下是 Java 中檔案處理的範例:

範例#1

在此範例中,程式使用各種方法來取得特定詳細資訊。在這個應用程式中,使用不同的方法來獲取與文件相關的信息,例如:

  1. 檢索檔案的絕對路徑。
  2. 檢查文件是否可寫入。
  3. 檢查文件是否可讀。
  4. 正在檢索檔案名稱。
  5. 正在檢索檔案大小。
  6. 取得指定目錄中存在的檔案清單等

代碼:

導入io包不同的類別。

import java.io.File;
import java.io.IOException;
public class FileHandlingExample2 {
public static void main(String[] args) {
// Creating an object of a file
File fileObj = new File("D:/Programs/fileHandlingOperations.txt");
if (fileObj.exists()) {
//retrieving the path of the specified file
System.out.println("\nSpecified file path: " + fileObj.getAbsolutePath());
//checking whether the file is writable or not
System.out.println("\nIs the file Writable: " + fileObj.canWrite());
//checking whether the file is Readable or not
System.out.println("\nIs the file Readable " + fileObj.canRead());
//retrieving file name
System.out.println("\nFile name: " + fileObj.getName());
//retrieving file size
System.out.println("\nFile size (in bytes) " + fileObj.length());
File fileDirObj = new File("D:/Programs/");
String[] fileList = fileDirObj.list();
//displaying here the list of files available in the directory
for (int i = 0; i < fileList.length; i++) {
System.out.print("\n" + fileList[i]);
}
System.out.println("\n");
}
else {
System.out.println("Specified file does not exist.");
}
}
}

輸出:

在上面給出的範例中,我們可以看到不同的方法如何提供執行與文件相關的不同檢查所需的資訊。

Java 中的檔案處理

範例#2

這個範例展示了程式中如何使用不同的方法來執行不同類型的操作。程式中使用exists()方法檢查檔案是否存在;之後,放置 if..else.. 條件。

In the If condition, it checks first whether the existing file is writable or not; if the existing file remains writable, then the code block under the if section uses the FileWriter class method to write content into the existing file.

Code:

Importing io package different classes.

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class FileHandlingExample {
public static void main(String[] args) {
try {
File fileObj = new File("D:/Programs/fileHandlingOperations.txt");
if(fileObj.exists()){
System.out.println("File already exists.");
if(fileObj.canWrite()){
//creating object of FileWriter class to write things on file
FileWriter fwObj = new FileWriter("D:/Programs/fileHandlingOperations.txt");
// Writes this content into the specified file
fwObj.write("It is a basic example of writing in file!");
//closing the files once writing completed
fwObj.close();
System.out.println("\nContent has been written to the file.");
}else{
System.out.println("\nFile is not in writable mode.");
}
;
}else{
if (fileObj.createNewFile()) {
System.out.println("New File created: " + fileObj.getName());
}
}
}
catch (IOException ioError) {
System.out.println("An error occurred.");
ioError.printStackTrace();
}
}
}

Output:

In the above-given example, After compilation, running the program the first time will create a file with the specified name in the program.

Java 中的檔案處理

Running the program a second time will write the content in the existing file.

Java 中的檔案處理

Conclusion

The article above explains what a file is, how to perform operations on it, and how file handling works. It was also demonstrated in the above section about classes & methods that can be used to work with files in java.

以上是Java 中的檔案處理的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn