Java-IO流-打印流-PrintStream&PrintWriter

PrintWriter

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class PrintWriterDemo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //讀取鍵盤錄入。將錄入的數(shù)據(jù)轉成大寫保存到文件中。
        
        BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));
        
        PrintWriter out = new PrintWriter(System.out,true);//true自動刷新,對println有效。
        
        String line = null;
        while((line=bufr.readLine())!=null){
            if("over".equals(line)){
                break;
            }
            out.println(line.toUpperCase());
//          out.flush();
        }
        
        out.close();
    
        
        //想要將數(shù)據(jù)打印到文件中,并使用自動刷新。
        //PrintWriter out = new PrintWriter(new FileWriter("a.txt"),true);
    }

}

PrintStream

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

public class PrintStreamDemo {

    /**
     * @param args
     * @throws IOException 
     */
    public static void main(String[] args) throws IOException {
        //需求:希望寫一個整數(shù),到目的地整數(shù)的表現(xiàn)形式不變。可以將整數(shù)轉成字符串在寫入到目的地。
        
//      FileOutputStream fos = new FileOutputStream("tempfile/int.txt");
//      fos.write(String.valueOf(97));//字節(jié)流的write方法只將一個整數(shù)的最低字節(jié)寫入到目的地;//00000000 00000000 00000001 01100001
//      fos.close();
        
        
//      FileOutputStream fos = new FileOutputStream("tempfile/int.txt");
//      //需要額外功能嗎?保證數(shù)據(jù)值的表示形式。需要。
//      PrintStream ps = new PrintStream(fos);
////        ps.write(97);// 只能寫入最低字節(jié)。
//      ps.print(97);//將數(shù)據(jù)轉成字符串在寫入。保證數(shù)據(jù)值的表現(xiàn)形式。
//      ps.close();
        
        
        PrintStream ps = new PrintStream("tempfile/int.txt");
        ps.print(98);
        ps.close();
    }

}
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容