国产精品天干天干,亚洲毛片在线,日韩gay小鲜肉啪啪18禁,女同Gay自慰喷水

歡迎光臨散文網(wǎng) 會員登陸 & 注冊

java字符輸出流PrintWriter

2022-08-21 13:30 作者:虛云幻仙  | 我要投稿

/**
* 測試字符輸出流PrintWriter
*/

public class TestPrintWriter {
? ?public static void main(String[] args) {
? ? ? ?try (BufferedReader br = new BufferedReader(new FileReader("iostream/testFile5.txt"));
? ? ? ? ? ? PrintWriter pw = new PrintWriter("iostream/testFile6.txt")){
? ? ? ? ? ?//PrintWriter為節(jié)點流,在實例化pw時生成了BufferedWriter(OutputStreamWriter(FileOutputStream(String fileName)))對象,實際依然使用了字節(jié)流轉(zhuǎn)字符流
? ? ? ? ? ?for (String line = br.readLine();line!=null;line = br.readLine()){
? ? ? ? ? ? ? ?pw.println(line);
? ? ? ? ? ? ? ?//使用PrintWriter拷貝字符串,.println()方法類似System.out.println,對每一行內(nèi)容輸出之后不需要手動newLine換行
? ? ? ? ? ?}
? ? ? ? ? ?pw.flush();
? ? ? ? ? ?//PrintWriter作為節(jié)點流時需要手動flush
? ? ? ?}catch (Exception e){
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ? ? ?
? ? ? ?try(BufferedReader br = new BufferedReader(new FileReader("iostream/testFile6.txt"));
? ? ? ? ? ?PrintWriter pw = new PrintWriter(new FileOutputStream("iostream/testFile7.txt"),true)){
? ? ? ? ? ?//PrintWriter作為處理流使用,將FileOutputStream對象傳入構造器,實例化pw時會外套BufferedWriter(OutputStreamWriter,將參數(shù)autoFlush設為true,每次println會自動flush
? ? ? ? ? ?for (String line = br.readLine();line!=null;line = br.readLine()){
? ? ? ? ? ? ? ?pw.println(line);
? ? ? ? ? ?}
? ? ? ? ? ?//無需flush,PrintWriter沒有緩沖,需要實例Buffered使用,如果構造器直接傳入Writer: new PrintWriter(new FileWriter("xxx"))這種情況不會外套BufferedWriter
? ? ? ?}catch (Exception e){
? ? ? ? ? ?e.printStackTrace();
? ? ? ?}
? ?}
}

java字符輸出流PrintWriter的評論 (共 條)

分享到微博請遵守國家法律
贡嘎县| 林周县| 宕昌县| 濮阳县| 龙泉市| 盐山县| 莱阳市| 乌拉特前旗| 辛集市| 东阿县| 古蔺县| 安国市| 河池市| 乌鲁木齐县| 东兴市| 桃江县| 偃师市| 福海县| 邻水| 保山市| 焦作市| 岫岩| 永兴县| 砀山县| 绵竹市| 独山县| 平泉县| 宁安市| 龙川县| 西峡县| 襄樊市| 同仁县| 丰都县| 五寨县| 河津市| 武穴市| 峨眉山市| 同心县| 安化县| 垣曲县| 美姑县|