码迷,mamicode.com
首页 > 其他好文 > 详细

IO流---打印流

时间:2018-07-27 01:35:21      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:print   刷新   out   readline   str   打印   lock   fileread   txt   

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流---打印流

标签:print   刷新   out   readline   str   打印   lock   fileread   txt   

原文地址:https://www.cnblogs.com/java-jiangtao-home/p/9374993.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!