java的PrintWriter打印流详解

-----------------------------java的PrintWriter打印流详解------------------------------------

 

打印流:只做输出没有输入

 

打印流分为字节打印流和字符打印流

 

PrintWriter:字符打印流

 

特点

1.    可以打印各种数据类型。

2.    封装了字符输出流,还可以做字符流和字节流的转换

3.    可以使用自动刷新。只有在调用println、printf或者format的其中一个方法时才可能完成此操作。

4.    可以直接向文件中写数据。

 

 

使用字符打印流向文件中写入数据

 

 

public  class PrintDemo {

 

    public  static  void main(String[] args) {

       

           PrintWriter pw = null;

        try {

           //创建字符打印流对象

           pw = new PrintWriter("e.txt");

           //向文件中分别打印boolean、字符、int、字符串

           pw.print(true);

           pw.print('a');

           pw.print(12);

           pw.print("李昆鹏");

           //刷新

           pw.flush();

        } catch (FileNotFoundException e) {

           e.printStackTrace();

        }finally {

           //字符打印流也是需要关闭的,但是不用处理异常

           if(pw != null)

           pw.close();

        }

       

    }

   

}

 

 

 

范例:从文件中读取数据并且打印在控制台

 

 

public  class PrintDemo2 {

 

    public  static  void main(String[] args) {

       

           BufferedReader br = null;

           PrintWriter pw = null;

        try {

           //创建高效缓冲区字符输入流对象

           br = new  BufferedReader(new FileReader("Student.txt"));

           //创建打印流对象

           //pw = new PrintWriter(System.out);

           //设置自动刷新的打印流在文件对象后面加true

           pw = new  PrintWriter(System.out,true);

           String line = null;

           while((line = br.readLine()) != null) {

               pw.println(line);

               //当使用自动刷新构造器时就不需要手动刷新

               //pw.flush();

           }

        } catch (FileNotFoundException e) {

           e.printStackTrace();

        } catch (IOException e) {

           e.printStackTrace();

        }finally {

           try {

               if(br != null)

               br.close();

               if(pw != null)

               pw.close();

           } catch (IOException e) {

               e.printStackTrace();

           }

          

        }

       

    }

   

}

 

 

 

范例:使用打印流来复制文本文件

 

public  class PrintDemo3 {

 

    public  static  void main(String[] args) {

       

           BufferedReader br = null;

           PrintWriter pw = null;

        try {

           //创建高效缓冲区字符输入流对象

           br = new  BufferedReader(new FileReader("Student.txt"));

           //创建打印流对象

           //pw = new PrintWriter(System.out);

           //设置自动刷新的打印流在文件对象后面加true

           pw = new PrintWriter(new FileWriter("Student1.txt"));

           String line = null;

           while((line = br.readLine()) != null) {

               pw.println(line);

               //当使用自动刷新构造器时就不需要手动刷新

               //pw.flush();

           }

        } catch (FileNotFoundException e) {

           e.printStackTrace();

        } catch (IOException e) {

           e.printStackTrace();

        }finally {

           try {

               if(br != null)

               br.close();

               if(pw != null)

               pw.close();

           } catch (IOException e) {

               e.printStackTrace();

           }

          

        }

       

    }

   

}

 

 

 

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值