在Java中將檔案中的所有小寫文字轉換為大寫文字?


在本文中,我們將學習如何在Java中將檔案中的所有小寫文字轉換為大寫文字。假設文字檔案包含以下資料:

“Merry and Jack are studying.”

然後,在用特定值更新文字檔案後,結果將是:

“MERRY AND JACK ARE STUDYING.”

此修改是藉助toUpperCase()方法完成的。它屬於Java中的String類。

讓我們深入探討這篇文章,瞭解如何使用Java程式語言來實現它。

舉幾個例子

示例1

假設檔案的原始內容為:

Merry and Jack are studying.

更新文字檔案後,結果將是:

MERRY AND JACK ARE STUDYING.

示例2

假設檔案的原始內容為:

Jack is riding the bicycle.

更新文字檔案後,結果將是:

JACK IS RIDING THE BICYCLE.

演算法

步驟1 - 將檔案路徑作為輸入。

步驟2 - 使用readLine()方法讀取輸入文字檔案的內容。

步驟3 - 使用toUpperCase()方法將oldContent中的所有小寫文字替換為大寫文字。

步驟4 - 使用FileWriter()方法用newContent重寫輸入文字檔案。

步驟5 - 列印結果。

語法

  • readLine() - 用於從控制檯讀取單行文字,它屬於Java的console類。

  • toUpperCase() - 它將所有小寫字母轉換為大寫字母。

  • write() - 用於為給定檔案寫入指定的字串,它屬於Java的Writer類。

  • close() - 用於關閉流並釋放流中任何繁忙的資源(如果存在流)。它屬於Java的Reader類。

多種方法

我們提供了多種解決方案。

  • 使用靜態使用者輸入

  • 使用使用者自定義方法

讓我們一一檢視程式及其輸出

注意 - 這些程式可能無法在任何線上Java編譯器上獲得預期的結果。因為線上編輯器無法訪問您本地系統的檔案路徑。

方法1:使用靜態使用者輸入

在這種方法中,檔案路徑將作為輸入。然後,根據演算法將檔案中的所有小寫文字更改為大寫文字。

示例

import java.io.*; public class Main{ public static void main(String[] args){ File fileToBeModified = new File("C:/Users/Kabir/Desktop/Work/file2.txt"); String oldContent = ""; BufferedReader reader = null; FileWriter writer = null; try{ reader = new BufferedReader(new FileReader(fileToBeModified)); String line = reader.readLine(); //Reading the content of input text file while (line != null) { oldContent = oldContent + line + System.lineSeparator(); line = reader.readLine(); } //printing the original content System.out.println("Original Content of the file: " + oldContent); //Replacing lowerCase text to upperCase text String newContent = oldContent.toUpperCase(); //Rewriting the input text file with newContent writer = new FileWriter(fileToBeModified); riter.write(newContent); //Printing the content of modified file //printing the content of the modified file System.out.println("New content of the file: " + newContent); } catch (IOException e){ e.printStackTrace(); } finally{ try{ //Closing the resources reader.close(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } }

輸出

Original Content of the file: Merry and Jack are studying.
New content of the file: MERRY AND JACK ARE STUDYING.

方法2:使用矩陣的動態初始化

在這種方法中,檔案路徑將作為輸入。然後呼叫使用者自定義方法,並根據演算法將檔案中的所有小寫文字更改為大寫文字。

示例

import java.io.*; public class Main { public static void main(String[] args){ //calling user defined method modifyFile("C:/Users/Kabir/Desktop/Work/file3.txt"); } //user defined method static void modifyFile(String filePath){ File fileToBeModified = new File(filePath); String oldContent = ""; BufferedReader reader = null; FileWriter writer = null; try{ reader = new BufferedReader(new FileReader(fileToBeModified)); //Reading the content of input text file String line = reader.readLine(); while (line != null) { oldContent = oldContent + line + System.lineSeparator(); line = reader.readLine(); } //printing the original content System.out.println("Original Content of the file: " + oldContent); //Replacing the lowerCase text to upperCase text String newContent = oldContent.toUpperCase(); //Rewriting the input text file with newContent writer = new FileWriter(fileToBeModified); //Printing the content of modified file writer.write(newContent); //printing the content of the modified file System.out.println("New content of the file: " + newContent); } catch (IOException e){ e.printStackTrace(); } finally{ try{ //Closing the resources reader.close(); writer.close(); } catch (IOException e){ e.printStackTrace(); } } } }

輸出

Original Content of the file: Jack is riding the bicycle.

New content of the file: JACK IS RIDING THE BICYCLE.

在本文中,我們探討了使用Java程式語言將檔案中的所有小寫文字轉換為大寫文字的不同方法。

更新於:2023年3月1日

3K+ 次瀏覽

啟動您的職業生涯

完成課程後獲得認證

開始
廣告