最美情侣中文字幕电影,在线麻豆精品传媒,在线网站高清黄,久久黄色视频

歡迎光臨散文網(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的評論 (共 條)

分享到微博請遵守國家法律
栾川县| 泸溪县| 武定县| 额济纳旗| 普安县| 陇西县| 龙胜| 亚东县| 西宁市| 华亭县| 南木林县| 库伦旗| 偏关县| 静海县| 西乡县| 大方县| 太康县| 青浦区| 南投县| 南木林县| 凉城县| 泰州市| 黄山市| 沁水县| 贺兰县| 交城县| 扎囊县| 台北县| 龙口市| 澄城县| 德清县| 偃师市| 万安县| 肃宁县| 石家庄市| 长葛市| 汽车| 昌黎县| 赤水市| 安多县| 高邮市|