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();
        }

    }
}
相关推荐
ps酷教程10 小时前
Jackson 解决没有无参构造函数的反序列化问题
java
NiceCloud喜云11 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
AI玫瑰助手11 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
油炸自行车11 小时前
Claude Code 错误:API Error: 400 Failed to deserialize the JSON body into the
开发语言·javascript·json·trae·claude code·api error 400
肩上风骋11 小时前
C++14特性
开发语言·c++·c++14特性
_日拱一卒12 小时前
LeetCode:994腐烂的橘子
java·数据结构·算法·leetcode·深度优先
隔窗听雨眠12 小时前
Nginx网关响应慢排查手记
java·服务器·nginx
智慧物业老杨12 小时前
智慧物业合同周期管理系统:从风险预警到智能交接的全流程数智化落地方案
java·人工智能·python
源码宝12 小时前
MES系统源码:Java8 + SpringBoot2.7 + MySQL8 + Redis,后端源码清爽易扩展
java·后端·源码·springboot·mes系统·源码二开·mes源码
JAVA社区13 小时前
Java高级全套教程(十)—— SpringCloudAlibaba超详细实战详解
java·开发语言·spring cloud·面试·职场和发展