Java的IO流-打印流

打印流

PrintStream

public void println()自动换行

PrintWriter

java 复制代码
package com.itheima.println;

import java.io.FileInputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

public class Test2 {
    public static void main(String[] args) {
        try (
                //1、创建一个打印流管道
                PrintWriter ps = new PrintWriter("IO/src/itheima08.txt");
              
        ){

            ps.println(97);
            ps.println("a");
            ps.print(true);
            ps.print("我是好人");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

输出语句的重定向

java 复制代码
package com.itheima.println;

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

public class Test4 {
    public static void main(String[] args) {
        System.out.println("老骥伏枥");
        System.out.println("志在千里");
        try (
                PrintStream ps = new PrintStream("IO/src/itheima09.txt");
                ){
            //把系统默认的打印流对象改成自己设置的打印流
            System.setOut(ps);
            System.out.println("烈士暮年");
            System.out.println("壮心不已");
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
相关推荐
萤丰信息29 分钟前
智慧园区能源革命:从“耗电黑洞”到零碳样本的蜕变
java·大数据·人工智能·科技·安全·能源·智慧园区
曹牧1 小时前
Eclipse为方法添加注释
java·ide·eclipse
是小胡嘛1 小时前
C++之Any类的模拟实现
linux·开发语言·c++
我叫张小白。1 小时前
Spring Boot拦截器详解:实现统一的JWT认证
java·spring boot·web·jwt·拦截器·interceptor
csbysj20202 小时前
Vue.js 混入:深入理解与最佳实践
开发语言
Gerardisite3 小时前
如何在微信个人号开发中有效管理API接口?
java·开发语言·python·微信·php
Want5953 小时前
C/C++跳动的爱心①
c语言·开发语言·c++
coderxiaohan4 小时前
【C++】多态
开发语言·c++
gfdhy4 小时前
【c++】哈希算法深度解析:实现、核心作用与工业级应用
c语言·开发语言·c++·算法·密码学·哈希算法·哈希
闲人编程4 小时前
Python的导入系统:模块查找、加载和缓存机制
java·python·缓存·加载器·codecapsule·查找器