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

Java PrintStream 類

在 Toto sisi 群組發布
你好!今天我們將討論 Java PrintStream 類及其所有功能。實際上,您已經熟悉PrintStream類的兩個方法。它們是print()println(),您可能每天都在使用它們 :) 因為System.out變量是一個PrintStream對象,所以當您調用System.out.println()時,您正在調用此類的方法之一。  PrintStream類的一般用途是將信息發送到某個流。 為什麼我們需要 PrintStream 類 - 1這個類有幾個構造函數。以下是一些最常用的:
  • PrintStream(OutputStream 輸出流)
  • PrintStream(File outputFile) 拋出 FileNotFoundException
  • PrintStream(String outputFileName) 拋出 FileNotFoundException
例如,我們可以將輸出文件的名稱傳遞給PrintStream構造函數。或者,我們可以傳遞一個File對象。讓我們看一些示例,看看它是如何工作的:

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.PrintStream; 

public class Main { 

   public static void main(String arr[]) throws FileNotFoundException 
   { 
       PrintStream filePrintStream = new PrintStream(new File("C:\\Users\\Username\\Desktop\\test.txt")); 

       filePrintStream.println(222); 
       filePrintStream.println("Hello world"); 
       filePrintStream.println(false); 

   } 
}
此代碼將在桌面上創建一個 test.txt 文件(如果它尚不存在)並依次將我們的數字、字符串和布爾值寫入其中。下面是我們運行程序後的文件內容:

222 
Hello world!
false
正如我們上面所說,您不必傳遞File對象。只需將文件路徑傳遞給構造函數就足夠了:

import java.io.FileNotFoundException; 
import java.io.PrintStream; 

public class Main { 

   public static void main(String arr[]) throws FileNotFoundException 
   { 
       PrintStream filePrintStream = new PrintStream("C:\\Users\\Username\\Desktop\\test.txt"); 

       filePrintStream.println(222); 
       filePrintStream.println("Hello world"); 
       filePrintStream.println(false); 
   } 
}
此代碼的作用與前面的代碼相同。另一個值得我們關注的有趣方法是printf(),它根據格式字符串生成輸出。什麼是“格式字符串”?讓我舉個例子吧:

import java.io.IOException; 
import java.io.PrintStream; 

public class Main { 

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

       PrintStream printStream = new PrintStream("C:\\Users\\Steve\\Desktop\\test.txt");

       printStream.println("Hello!"); 
       printStream.println("I'm a robot!"); 

       printStream.printf("My name is %s. I am %d!", "Amigo", 18); 

       printStream.close(); 
   } 
}
在這裡,我們沒有在字符串中明確說明機器人的姓名和年齡,而是為這些信息放置了佔位符,用%s%d表示。我們將替換它們的數據作為參數傳遞。在我們的例子中,這是字符串“ Amigo ”和數字 18。我們可以創建另一個佔位符,例如%b,並傳遞另一個參數。我們為什麼需要這個? 最重要的是,為了更大的靈活性。 如果您的程序要求您經常顯示歡迎消息,您將不得不手動為每個新機器人鍵入必要的文本。你甚至不能讓這個文本成為常量,因為每個人都有不同的名字和年齡!但是使用這個新方法,您可以將問候語隔離在一個常量中,如有必要,只需更改傳遞給printf()方法的參數即可。

import java.io.IOException; 
import java.io.PrintStream; 

public class Main { 

   private static final String GREETINGS_MESSAGE = "My name is %s. I am %d!"; 

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

       PrintStream printStream = new PrintStream("C:\\Users\\Steve\\Desktop\\test.txt"); 

       printStream.println("Hello!"); 
       printStream.println("We are robots!"); 


       printStream.printf(GREETINGS_MESSAGE, "Amigo", 18); 
       printStream.printf(GREETINGS_MESSAGE, "R2-D2", 35); 
       printStream.printf(GREETINGS_MESSAGE, "C-3PO", 35); 

       printStream.close(); 
   } 
} 

替換 System.in

在本課中,我們將“對抗系統”並學習如何替換 System.in變量,以便將系統輸出重定向到我們想要的任何位置。您可能會忘記System.in是什麼,但沒有任何 CodeGym 學生會忘記這個結構:

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.in (就像System.out  一樣)是System類的靜態變量。但與System.out不同的是,它引用了另一個類,即InputStream。默認情況下,System.in是一個從系統設備(鍵盤)讀取數據的流。但是,就像System.out一樣,我們可以將鍵盤替換為數據源。我們可以從任何地方讀取數據!讓我們看一個例子:

import java.io.*; 

public class Main { 

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

       String greetings = "Hi! My name is Amigo!\nI'm learning Java on the CodeGym website.\nOne day I will become a cool programmer!\n"; 
       byte[] bytes = greetings.getBytes(); 

       InputStream inputStream = new ByteArrayInputStream(bytes); 

       System.setIn(inputStream); 

       BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 

       String str; 

       while ((str = reader.readLine())!= null) { 

           System.out.println(str); 
       } 

   } 
}
那麼我們做了什麼? System.in通常綁定到鍵盤。但是我們不想從鍵盤讀取數據:讓我們從一個普通的字符串中讀取數據吧!我們創建了一個字符串並將其作為字節數組獲取。為什麼我們需要字節?問題是InputStream是一個抽像類,所以我們不能直接創建它的實例。我們必須選擇它的後代之一。例如,我們可以選擇ByteArrayInputStream。它很簡單,光是它的名字就告訴我們它是如何工作的:它的數據源是一個字節數組。所以我們創建一個字節數組並將其傳遞給將讀取數據的 流的構造函數。現在一切都準備好了!現在我們只需要使用 System.setIn ()顯式設置in變量值的方法。如果沒有out,您會記得,也無法直接設置變量的值:我們必須使用setOut()方法。在我們將我們的 InputStream 分配給System.in變量之後,我們想要檢查我們是否達到了我們的目的。我們的老朋友BufferedReader在這裡幫了我們的忙。通常,此代碼會在 IntelliJ IDEA 中打開控制台,然後讀取您從鍵盤輸入的數據。

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); 

       String str; 

       while ((str = reader.readLine())!= null) { 

           System.out.println(str); 
       }
但是現在當你運行它時,你會看到我們的字符串只是簡單地顯示在控制台中。鍵盤沒有讀數。我們更換了數據源。它不再是鍵盤,而是我們的字符串!就這麼簡單 :) 在今天的課程中,我們了解了一個新類,並探索了一個使用 I/O 的新小技巧。現在是時候返回課程並完成一些任務了 :) 下節課見!
留言
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION