JAVA學習筆記(三十四)- 位元組列印流 PrintStream

來源:互聯網
上載者:User

標籤:位元組列印流   重新導向標準輸入輸出   

PrintStream位元組列印流
import java.io.BufferedReader;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintStream;import java.io.PrintWriter;import java.io.Reader;/* * PrintStream位元組列印流,輸出資料更簡單 */public class Test02 {    public static void main(String[] args) throws IOException {        test1();    }    // PrintStream    public static void test1() throws IOException {        // 定義一個檔案位元組輸出資料流        FileOutputStream fos = new FileOutputStream("D:\\Java\\itany.txt");        // 定義一個位元組列印流        PrintStream ps = new PrintStream(fos, true);        ps.println(25);        ps.println("哈哈");        ps.println("嘻嘻");        ps.print(true);        ps.print(13.5);        ps.append(‘a‘);        // ps.flush();        System.out.println("列印資料成功!");        ps.close();    }    //PrintWriter    public static void test2() throws IOException{        //定義一個檔案字元輸出資料流        FileWriter fw=new FileWriter("D:\\Java\\itany.txt");        //定義一個字元列印流        PrintWriter pw=new PrintWriter(fw);        //列印9*9乘法表        for(int i=1;i<=9;i++){            for(int j=1;j<=i;j++){                pw.print(i+"*"+j+"="+(i*j)+"\t");            }            pw.println();        }        pw.flush();        pw.close();        fw.close();        //讀取資料        BufferedReader reader=new BufferedReader(new FileReader("D:\\Java\\itany.txt"));        String str=null;        while((str=reader.readLine())!=null){            System.out.println(str);        }        reader.close();    }}
重新導向標準輸入輸出
import java.io.FileInputStream;import java.io.IOException;import java.io.InputStreamReader;import java.io.PrintStream;/* * 重新導向標準輸入輸出 *  * 預設輸入裝置為鍵盤 * 預設輸出裝置為顯示器 */public class Test03 {    public static void main(String[] args) throws IOException {        //從預設輸入裝置擷取一個位元組的資料        /*int data=System.in.read();        System.out.println((char)data);*/        //將標準輸入裝置重新導向到檔案        FileInputStream fis=new FileInputStream("D:\\Java\\itany.txt");        System.setIn(fis);//重新導向輸入        //將標準轉出裝置重新導向到PrintStream        PrintStream ps=new PrintStream("D:\\Java\\hello.txt");        System.setOut(ps);//重新導向輸出        //調用標準輸入裝置讀取資料        int data=System.in.read();        while(data!=-1){            //調用標準輸出裝置列印資料            System.out.write((char)data);            //System.out.print((char)data);            data=System.in.read();            System.out.flush();        }        System.in.close();        System.out.close();        fis.close();        ps.close();    }}

JAVA學習筆記(三十四)- 位元組列印流 PrintStream

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.