在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(); } } }
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(); } } }
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(); } } }
定向輸入輸出
通過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中 */

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(); } }

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(); } }

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(); } }