IO流---打印流
Posted java-jiangtao-home
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了IO流---打印流相关的知识,希望对你有一定的参考价值。
package 打印流; import java.io.FileNotFoundException; import java.io.PrintWriter; public class Demo1 { public static void main(String[] args) { PrintWriter pw = null; try { pw = new PrintWriter("c.txt"); pw.print("aaa"); pw.print(‘c‘); pw.print(true); pw.println(1); pw.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { if(pw!=null) { pw.close(); } } } }
package 打印流; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.PrintWriter; public class Demo2 { public static void main(String[] args) { PrintWriter pw = null; BufferedReader bw = null; try { pw = new PrintWriter(System.out); //自动刷新 pw = new PrintWriter(System.out,true); bw = new BufferedReader(new FileReader("a.txt")); String line = null; while((line = bw.readLine()) != null) { pw.println(line); // 自动刷新就不用flush了 //pw.flush(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();} finally { if(bw!=null) { pw.close(); } if(pw!=null) { pw.close(); } } } }
package 打印流; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class Demo3 { public static void main(String[] args) { PrintWriter pw = null; BufferedReader bw = null; try { // pw = new PrintWriter(System.out); //自动刷新 /** * PrintFile里面的参数如果是new FileWriter(""),就是往文件里面打印东西,如果是 * System.out就是往控制台打印东西 */ pw = new PrintWriter(new FileWriter("student.txt"),true); bw = new BufferedReader(new FileReader("a.txt")); String line = null; while((line = bw.readLine()) != null) { pw.println(line); // 自动刷新就不用flush了 //pw.flush(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace();} finally { if(bw!=null) { pw.close(); } if(pw!=null) { pw.close(); } } } }
以上是关于IO流---打印流的主要内容,如果未能解决你的问题,请参考以下文章
java缓冲字符字节输入输出流:java.io.BufferedReaderjava.io.BufferedWriterjava.io.BufferedInputStreamjava.io.(代码片段