java學習筆記 打印流Print流


在io包里,打印流是輸出最方便的類

主要包含字節打印流PrintStream,字符打印流PrintWriter

PrintStream是OutputStream的子類,把一個輸出流的實例傳遞到打印流之后,可以更加方便地輸出內容,相當於打印流把輸出流重新包裝一下

PrintStream類的print()方法被重載很多次print(int i)、print(boolean b)、print(char c)

PrintStream操作實例

import java.io.*;

public class Test {
    public static void main(String[] args) {
        //PrintStream流輸出
        try{
            File f1 = new File("e:\\java\\aaa.txt");
            File f2 = new File("e:\\java\\bbb.txt");
            PrintStream ps1 = new PrintStream(f1);
            PrintStream ps2 = new PrintStream(f2);
            ps1.println("24");
            ps1.print("1 + 1 =" + 2);//更加方便的輸出內容
            ps2.print(24);//此方法被重載很多次
            ps1.close();
            ps2.close();
        }catch(IOException e) {
            e.printStackTrace();    
        }
        
        //PrintStream流printf方法格式化輸出
        try{
            String name = "寶寶";
            int age = 24;
            float high = 1.64f;
            char sex = 'M';
            PrintStream ps3 = new PrintStream(new FileOutputStream(new File("e:\\java\\ccc.txt")));
            ps3.printf("姓名: %s; 年齡: %d; 身高 %f; 性別: %c",name,age,high,sex);
        }catch(IOException e){
            e.printStackTrace();    
        }
    }    
}

r1

System類對打印流的支持

System類的三個常量:

   public static final PrintStream out

   public static final PrintStream err

   public static final InputStream in

system.out是PrintStream的對象

import java.io.*;

public class Test {
    public static void main(String[] args) {
        //使用OutputStream對屏幕上進行輸出
        try{
            OutputStream ops = System.out;
            ops.write("Hello Word!".getBytes());
            ops.close();
        }catch(IOException e) {
            e.printStackTrace();    
        }
    }    
}

r1

import java.io.*;

public class Test {
    public static void main(String[] args) {
        //讀取鍵盤上的輸入內容
        try{
            InputStream br = System.in;
            System.out.print("請輸入內容:");
            byte b[] = new byte[1024];
            int len = br.read(b);
            System.out.println("鍵盤輸入的是: "+ new String(b,0,len));
            br.close();
        }catch(IOException e) {
            e.printStackTrace();    
        }
    }    
}

r2

定向輸入輸出

通過System類可以改變out err in的位置

public static void setOut(PrintStream out)

public static void setIn(PrintStream in)

public static void setErr(PrintStream err)

setOut操作

import java.io.*;

public class Test {
    public static void main(String[] args) {
        //讀取鍵盤上的輸入內容
        try{
            System.setOut(new PrintStream("e:\\java\\aaa.txt"));
            System.out.println("寶寶");
        }catch(IOException e) {
            e.printStackTrace();    
        }
    }    
}

/*
1.此程序運行后,不會再屏幕上顯示,而是儲存到aaa.txt中
*/

r1

 

import java.io.*;

public class Test {
    public static void main(String[] args)throws Exception {
        //測試寫入char型和int型
        PrintStream ps1 = null;
        ps1 = new PrintStream("e:\\java\\aaa.txt");
        char c;
        int len = 0;
        for(c = 0;c <= 60000;c++) {
            ps1.print(c);//print(char c)Prints a character.在文件中寫入的是字符。大小82.9kb
            len++;
            if (len >= 100) {
                len = 0;
                ps1.println();    
            }    
        }
        
        FileWriter fw = null;
        fw = new FileWriter("e:\\java\\bbb.txt");
        int i;
        len = 0;
        for(i = 0;i<= 60000;i++) {
            fw.write(i);//write(int c)Writes a single character.在文件中寫入的是字符,和aaa.txt一樣。大小82.3kb
            len++;
            if(len >= 100) {
                len = 0;
                fw.write(" ");    
            }    
        }
        
        PrintStream ps2 = null;
        ps2 = new PrintStream("e:\\java\\ccc.txt");
        int j;
        len = 0;
        for(j = 0;j <= 60000;j++) {
            ps2.print(j);//print(int i)Prints an integer.寫入的是整形。大小283kb
            len++;
            if(len >= 100) {
                len = 0;
                ps2.println();    
            }
        }
        
        ps1.close();
        ps2.close();
        fw.close();
    }    
}
View Code
import java.io.*;

public class Test {
    public static void main(String[] args)throws Exception {
        //按行讀一個文件,輸出到屏幕
        BufferedReader br = new BufferedReader(new FileReader("e:\\java\\pkg1\\Test.java"));
        PrintStream ps = System.out;
        String str = br.readLine();
        while((str = br.readLine()) != null) {
            ps.println(str);        
        }
        br.close();
        ps.close();
    }    
}
View Code
import java.io.*;
import java.util.*;

public class Test {
    public static void main(String[] args)throws Exception {
        //存儲鍵盤輸入內容,並加上日期,相當於記錄日志。
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        FileWriter fw = new FileWriter("e:\\java\\a.txt",true);//可以追加輸入
        String str = null;
        while((str = br.readLine()) != null) {//阻塞式方法,從標准輸入System.in鍵盤輸入。只有當我們敲回車后這局話才返回,才會執行。
            if(str.equalsIgnoreCase("exit")) {
                //System.exit;    
                break;
            }
            fw.write(str + "\r\n" );
            fw.flush();
                
        }
        fw.write("-----------------" + new Date() + "----------------" +"\r\n");
        br.close();
        fw.close();
    }    
}
View Code

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM