IO_File类使用:打印流

PrintStream  字节打印流
PrintWriter  字符打印流
主要作用:能更方便的用于输出内容
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.Writer;

public class PrintStreamDemo {

    public static void main(String[] args) {
        byteprint();
        charprint();
    }
    
    //字节打印流
    public static void byteprint() {
        File file = new File("E:\\Java_IO\\file4.txt");
        try {
            
            OutputStream out = new FileOutputStream(file);            //字节输出流
            BufferedOutputStream bos = new BufferedOutputStream(out); //字节输出缓存流
            PrintStream ps = new PrintStream(bos);                    //字节打印流
                                                                      //无非是增加了两个套路而已,实际就是字节出流本身的增强而已
            ps.print("我爱我家");//字节打印流写入内容到文件
            ps.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    //字符打印流
    public static void charprint() {
        File file = new File("E:\\Java_IO\\file4.txt");
        try {
            Writer write = new FileWriter(file);            //字符输出流
            BufferedWriter bw = new BufferedWriter(write);  //字符输出缓存流
            PrintWriter pw = new PrintWriter(bw);           //字符打印流
                                                            //无非是增加了两个套路而已,实际就是字符出流本身的增强而已
            pw.print("胜似仙居"); //字符打印流写入内容到文件
            pw.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

猜你喜欢

转载自blog.csdn.net/weixin_34090643/article/details/87073960