CodeGym/Java Blog/Toto sisi/Java PrintWriter 類
John Squirrels
等級 41
San Francisco

Java PrintWriter 類

在 Toto sisi 群組發布
個成員

什麼是 Java 中的 PrintWriter 類?

“PrintWriter 是一個類,用於在控制台或 Java 文件中以文本形式寫入任何形式的數據,例如 int、float、double、String 或 Object。”
例如,您可以使用 PrintWriter 對象將數據記錄在文件中或在控制台上打印。

如果其他 OutputStreams 可用,為什麼要使用 PrintWriter?

在控制台上打印數據的最常見做法是使用System.out.print方法。但是,在使用PrintWriter對象發布全球應用程序時,更容易根據指定的Locale(區域標準)自定義格式。我們可以在本文後面根據您的系統研究使用 Locale。

如何使用 PrintWriter 類?

要使用PrintWriter,您需要導入java.io.PrintWriter類。然後在初始化其對像後,您可以根據需要將其用於在控制台或文件中寫入。讓我們看看控制台和文件的PrintWriter類的兩種初始化方式。存在多個不同的構造函數。但在這裡,我們將首先向您介紹最簡單的。

使用 PrintWriter 的控制台輸出

以下是一個PrintWrtier對象,用於在控制台上打印文本。
PrintWriter consoleOutput = new PrintWriter(System.out);
這裡System.out對像被傳遞給構造函數以在控制台上寫入。

使用 PrintWriter 輸出文件

這是在文件中寫入文本的 PrintWriter對象。
PrintWriter fileOutput = new PrintWriter("FileOutput.txt");
此構造函數將String輸入作為文件名。創建指定名稱的文件並在其中寫入文本數據。

PrintWriter 類的方法

Java PrintWriter類帶有許多方便的方法。僅僅通過徵募他們就更難接受。因此,讓我們逐一查看示例。它們是什麼,以及我們如何輕鬆地使用它們。

示例 1

此示例將演示如何使用PrintWriter對像在控制台上進行打印。
import java.io.PrintWriter;

public class PrintWriterDemo {

	public static void main(String[] args) throws Exception {


            // by importing the java.io.PrintWriter class
		PrintWriter consoleOutput = new PrintWriter(System.out);

		consoleOutput.printf("Hey there! This is %S.\n", "Lubaina Khan");
		consoleOutput.print("Today you're exploring the PrinWriter class with Code Gym. ");
		consoleOutput.println("Hope you're having fun!");
		consoleOutput.append("Patience is the key when learning new concepts.\n");
		consoleOutput.append("It all boils down to practise and persistence. :)");

		consoleOutput.flush();
		consoleOutput.close();
	}
}

輸出

嘿!這是盧拜娜汗。今天,您將使用 Code Gym 探索 PrinWriter 類。希望你玩得開心!耐心是學習新概念的關鍵。這一切都歸結為實踐和堅持。:)

PrintWriter 類的使用方法

printf(String str, Object arguments);
printf ()方法採用要打印的字符串的格式。在這裡,佔位符%S被字符串旁邊傳遞的大寫參數替換。
print(String str);
此方法將使用PrintWriter對 像打印傳遞給它的字符串。
println(String str);
在字符串內容之後打印一個換行符。
append(CharSequence cs);
傳遞給 append 的字符序列被添加到PrintWrtier對象。
flush();
清空PrintWriter對象 的內容。
close();
關閉寫入流並釋放所有分配的資源。

示例 2

此示例將展示使用PrintWriter類將數據寫入文件。
import java.io.PrintWriter;
import java.util.Date;
import java.util.Locale;

public class PrintWriterDemo {

	public static void main(String[] args) throws Exception {

		try {
                  // by importing the java.io.PrintWriter class
			PrintWriter fileOutput = new PrintWriter("FileOutput.txt");

		      fileOutput.printf(Locale.getDefault(), "Hi, What's the day today? %s.\n", new Date());

			fileOutput.print("Here's an implementation of PrinWriter class for file writing.\n");
			fileOutput.println("Hope Code Gym made it simpler for you to understand.");
			fileOutput.append("One step at a time, and off you go!", 0, 35);

			fileOutput.flush();
			fileOutput.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

輸出

嗨,今天是星期幾?太平洋標準時間 2021 年 7 月 25 日星期日 17:30:21。這是用於文件寫入的 PrinWriter 類的實現。希望 Code Gym 讓您更容易理解。一步一個腳印,然後出發!

PrintWriter 類的使用方法

讓我們討論用於文件寫入的方法,這些方法有別於在控制台上寫入。
printf(Locale locale, String str, Object args);
在這裡你可以傳遞語言環境(我們使用系統默認值來保持一致性)你可以根據需要使用任何語言環境。這符合任何基於區域的格式。其餘實現與之前使用的相同。
append(CharSequence cs, int beginningIndex, int endingIndex);
您可以通過指定其開始和結束索引來添加傳遞的CharSequence 的塊。這裡我們使用了最後一個索引。您可以嘗試使用它來查看不同的輸出。
try{
   ...
} catch (Exception e){
   ...
}
try-catch 塊在文件寫入中是必須使用的。這是為了避免在系統上訪問(例如,權限問題)或創建文件時出現任何異常。

結論

這是對使用 Java PrintWriter類的快速介紹。希望這對你來說不是壓倒性的。如果是,我們建議您理解每個標題並在進行時進行練習。如有任何障礙,隨時歡迎您回來。不斷質疑,不斷成長。
留言
  • 受歡迎
你必須登入才能留言
此頁面尚無留言