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