打印流PrintStream类和PrintWriter类

打印流 PrintStreamPrintWriter

打印流只有输出流,没有输入流

PrintStream

继承关系图及构造器

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-V0eCRx9W-1646659497587)(E:\Typora笔记\java笔记\img\image-20220306213708642.png)]

package IO_.print_;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;

/**
 * @author: 海康
 * @version: 1.0
 */
public class PrintStream1 {
    public static void main(String[] args) throws IOException {
        PrintStream out = System.out;

        // 默认的打印方式:是显示器
        out.print("海康");// 底层默认调用的是 write方法
        out.println(168);
        out.print('E');


        //在默认情况下,PrintStream 输出数据的位置是 标准输出,即显示器
        /*
        public void print(String s) {
            if (s == null) {
            s = "null";
            }
            write(s);
        }
        */
        // 由于底层源码 调用是 write 方法,所以可以直接调用 write 的作用效果是一样的,下面这语句和上面“打出海康一样”
        out.write("广州".getBytes());

        out.close();// 关闭流

        /**
         * 修改打印方式:
         * 可以通过 setOut方法
         * 修改打印 的输出的方式,默认是显示器
         */
        // 重定向输出设备到文件中
        System.setErr(new PrintStream("e:\\IDEACODE\\javase\\1.txt"));
        // 写入的内容
        System.out.println("湛江");
    }
}

package com.hspedu.printstream;

import java.io.IOException;
import java.io.PrintStream;

/**
 * 演示PrintStream (字节打印流/输出流)
 */
public class PrintStream_ {
    public static void main(String[] args) throws IOException {

        PrintStream out = System.out;
        //在默认情况下,PrintStream 输出数据的位置是 标准输出,即显示器
        /*
             public void print(String s) {
                if (s == null) {
                    s = "null";
                }
                write(s);
            }

         */
        out.print("john, hello");
        //因为print底层使用的是write , 所以我们可以直接调用write进行打印/输出
        out.write("湛江,你好".getBytes());
        out.close();

        //我们可以去修改打印流输出的位置/设备
        //1. 输出修改成到 "e:\\f1.txt"
        //2. "hello, 湛江~" 就会输出到 e:\f1.txt
        //3. public static void setOut(PrintStream out) {
        //        checkIO();
        //        setOut0(out); // native 方法,修改了out
        //   }
        System.setOut(new PrintStream("e:\\f1.txt"));
        System.out.println("hello, 湛江~");


    }
}
PrintWrite

继承关系图

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-CkEZGtQR-1646659497588)(E:\Typora笔记\java笔记\img\image-20220306215214432.png)]

构造器

Constructor and Description
PrintWriter(File file) 使用指定的文件创建一个新的PrintWriter,而不需要自动的线路刷新。
PrintWriter(File file, String csn) 使用指定的文件和字符集创建一个新的PrintWriter,而不需要自动进行线条刷新。
PrintWriter(OutputStream out) 从现有的OutputStream创建一个新的PrintWriter,而不需要自动线路刷新。
PrintWriter(OutputStream out, boolean autoFlush) 从现有的OutputStream创建一个新的PrintWriter
PrintWriter(String fileName) 使用指定的文件名创建一个新的PrintWriter,而不需要自动执行行刷新。
PrintWriter(String fileName, String csn) 使用指定的文件名和字符集创建一个新的PrintWriter,而不需要自动线路刷新。
PrintWriter(Writer out) 创建一个新的rintWriter,没有自动线冲洗。
PrintWriter(Writer out, boolean autoFlush) 创建一个新的PrintWriter
package com.hspedu.transformation;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 演示 PrintWriter 使用方式
 */
public class PrintWriter_ {
    public static void main(String[] args) throws IOException {

        //PrintWriter printWriter = new PrintWriter(System.out);
        PrintWriter printWriter = new PrintWriter(new FileWriter("e:\\f2.txt"));
        printWriter.print("hi, 北京你好~~~~");
        printWriter.close();//flush + 关闭流, 才会将数据写入到文件..

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值