zoukankan      html  css  js  c++  java
  • 打印流

             打印流

    打印流根据流的分类:

    l  字节打印流  PrintStream

    l  字符打印流  PrintWriter

    复制代码
     /* 
     * 需求:把指定的数据,写入到printFile.txt文件中
     * 
     * 分析:
     *     1,创建流
     *     2,写数据
     *     3,关闭流
     */
    public class PrintWriterDemo {
        public static void main(String[] args) throws IOException {
            //创建流
            //PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"));
            PrintWriter out = new PrintWriter("printFile.txt");
            //2,写数据
            for (int i=0; i<5; i++) {
                out.println("helloWorld");
            }
            //3,关闭流
            out.close();
        }
    }
    复制代码

                    打印流完成数据自动刷新

    可以通过构造方法,完成文件数据的自动刷新功能

    l  构造方法:

    l  开启文件自动刷新写入功能

    public PrintWriter(OutputStream out, boolean autoFlush)

    public PrintWriter(Writer out, boolean autoFlush)

    复制代码
     /* 
     * 分析:
     *     1,创建流
     *     2,写数据
     */
    public class PrintWriterDemo2 {
        public static void main(String[] args) throws IOException {
            //创建流
            PrintWriter out = new PrintWriter(new FileWriter("printFile.txt"), true);
            //2,写数据
            for (int i=0; i<5; i++) {
                out.println("helloWorld");
            }
            //3,关闭流
            out.close();
        }
    }
    复制代码

    复制文件

    复制代码
    public class Demo02 {
    public static void main(String[] args) throws IOException {
        //文件复制
        FileReader fr=new FileReader("D:\io0512\print.txt");
        //添加缓冲流
        BufferedReader br=new BufferedReader(fr);
        //明确目的地
        FileWriter fw=new FileWriter("D:\io0512\a\print.txt");//续写加true
        //添加打印流
        PrintWriter pw=new PrintWriter(fw,true);//开启文件自动刷新写入功能
        //开始复制
        String len=null;
        while((len=br.readLine())!=null){
            pw.println(len);
        }
        br.close();
        pw.close();
        
    }
    }
    复制代码
  • 相关阅读:
    计算机专业找工作注意什么
    LU分解
    HDU2050
    牛牛与字符串border 题解(gcd)
    牛牛与交换排序 题解(双端队列模拟区间反转)
    动态最小生成树 题解(线段树+归并排序)
    系数 题解(lucas+思维)
    D. Dogeforces 题解(并查集+构造)
    Java 入土基础
    E. AZ Graph 题解(思维)
  • 原文地址:https://www.cnblogs.com/marswenze/p/13426098.html
Copyright © 2011-2022 走看看