打印流

1 打印流的概述

打印流添加输出数据的功能,可以打印各种数据值表现形式

2打印流的特点

2.1 只有输出目的

2.2 永远不会抛出io异常

3.打印流的分类

分为:字节打印流 PrintStream  和  字符打印流  PrintWriter

方法有两个:

void print(String str): 输出任意类型的数据

void println(String str): 输出任意类型的数据,自动写入换行操作

复制代码
    public static void method01() throws FileNotFoundException{
        //明确目的地
        FileOutputStream fos=new FileOutputStream("E:\java\print.txt",true);
        PrintStream ps=new PrintStream(fos);
        //原样输出
        ps.print(100);
        ps.println("你好");
        ps.write(100);
        ps.close();
        
    }
复制代码

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

构造方法:

public PrintWriter(OutputStream out, boolean autoFlush)

public PrintWriter(Writer out, boolean autoFlush)

复制代码
    public static void method02() throws FileNotFoundException{
        FileOutputStream fos=new FileOutputStream("E:\java\print.txt");
        //创建自动刷新的字符打印流
        PrintWriter pw=new PrintWriter(fos,true);
        pw.println("你好");
        pw.print("java");
        pw.close();
    }
复制代码

可以通过打印流来完成文件的复制

复制代码
    public static void copy() throws IOException{
        //明确数据源
        FileReader fr=new FileReader("E:\java\print.txt");
        BufferedReader br=new BufferedReader(fr);
        //明确目的地
        FileWriter fw=new FileWriter("E:\java\a\print.txt");
        PrintWriter pw=new PrintWriter(fw,true);
        String line="";
        while((line=br.readLine())!=null){
            pw.println(line);
        }
        //释放资源
        br.close();
        pw.close();
        
    }
原文地址:https://www.cnblogs.com/lxzwhite/p/10687527.html