Print流(只有输出流没有输入流)
PrintWriter和PrintStream都属于输出流,分别针对于字符和字节
PrintWriter和PrintStream提供了重载的print方法
Println方法用于多种数据类型的输出
PrintWriter和PrintStream的输出操作不会抛出异常,用户通过检测错误状态获取错误信息
PrintWriter和PrintStream有自动flush方法
构造方法:
PrintWriter(File file) Creates a new PrintWriter, without automatic line flushing, with the specified file. PrintWriter(File file, String csn) Creates a new PrintWriter, without automatic line flushing, with the specified file and charset. PrintWriter(OutputStream out) Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream. PrintWriter(OutputStream out, boolean autoFlush) Creates a new PrintWriter from an existing OutputStream. PrintWriter(String fileName) Creates a new PrintWriter, without automatic line flushing, with the specified file name. PrintWriter(String fileName, String csn) Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset. PrintWriter(Writer out) Creates a new PrintWriter, without automatic line flushing. PrintWriter(Writer out, boolean autoFlush) Creates a new PrintWriter. PrintStream(File file) Creates a new print stream, without automatic line flushing, with the specified file. PrintStream(File file, String csn) Creates a new print stream, without automatic line flushing, with the specified file and charset. PrintStream(OutputStream out) Creates a new print stream. PrintStream(OutputStream out, boolean autoFlush) Creates a new print stream. PrintStream(OutputStream out, boolean autoFlush, String encoding) Creates a new print stream. PrintStream(String fileName) Creates a new print stream, without automatic line flushing, with the specified file name. PrintStream(String fileName, String csn) Creates a new print stream, without automatic line flushing, with the specified file name and charset.
例子一
public static void main(String[] args) { try { PrintStream ps = null; FileOutputStream fos = new FileOutputStream("d:\\bak\\log.dat"); ps = new PrintStream(fos); if(ps != null) { System.setOut(ps);//把ps设置成System.out.println();不再输出在控制台了 } int ln = 0; for(char c = 0; c <= 60000; c++) { System.out.print(c + ""); if( ln++ >= 100) { System.out.println(); ln = 0; } } } catch (Exception e) { e.printStackTrace(); } }
结果:d:\\bak\\log.dat,log.data文件
例子二、
public static void main(String[] args) { try { PrintStream ps = System.out; BufferedReader br = new BufferedReader(new FileReader("d:\\bak\\log.dat")); String s = null; while((s = br.readLine()) != null) { ps.println(s); } br.close(); } catch (IOException e) { e.printStackTrace(); } }
结果:
例子三、
public static void main(String[] args) { try { String s = null; BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); FileWriter fw = new FileWriter("d:\\bak\\logfile.log", true);//日志 PrintWriter pw = new PrintWriter(fw); while ((s = br.readLine()) != null) { if("exit".equalsIgnoreCase(s)) break; System.out.println(s.toUpperCase()); pw.println("--------"); pw.println(s.toUpperCase()); pw.flush(); } pw.println("=====" + new Date() + "======"); pw.flush(); pw.close(); } catch (IOException e) { e.printStackTrace(); } }
结果:
控制台: 12121212121 12121212121 adasdasdasdasd ADASDASDASDASD exit
文件:d:\\bak\\logfile.log,logfile.log文件
时间: 2024-10-17 09:59:48