• 打印流


    1、打印流基本使用

    package demo02;
    
    import org.junit.Test;
    
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    import java.io.PrintWriter;
    
    /**
     * @description: demo10
     * @author: liuyang
     * @create: 2021-09-06 22:27
     */
    public class Demo10 {
        /**
         * 重定向System.out打印流
         */
        @Test
        public void test1() {
            FileOutputStream fileOutputStream = null;
            PrintStream ps = null;
            try {
                fileOutputStream = new FileOutputStream("text5.txt");
                ps = new PrintStream(fileOutputStream, true);
                System.setOut(ps);
                System.out.println("hello,我是中国人");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 打印流PrintStream
         */
        @Test
        public void test2() {
            FileOutputStream fileOutputStream = null;
            PrintStream ps = null;
            try {
                fileOutputStream = new FileOutputStream("text5.txt");
                ps = new PrintStream(fileOutputStream, true);
                ps.print("你们好");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (ps != null) {
                        ps.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    
        /**
         * 打印流:PrintWriter
         */
        @Test
        public void test3() {
            FileOutputStream fileOutputStream = null;
            PrintWriter pw = null;
            try {
                fileOutputStream = new FileOutputStream("text5.txt");
                pw = new PrintWriter(fileOutputStream, true);
                pw.print("你们好吗?");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (pw != null) {
                        pw.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
    相识是缘
  • 相关阅读:
    Devexpress之LayoutControl的使用及其控件布局设计
    C#入门笔记3 表达式及运算符2
    C#入门笔记3 表达式及运算符
    C#入门笔记2 变量
    C#入门笔记1
    Devexpress之GridControl显示序列号
    C++学习之重载运算符1
    解决"找不到该项目”无法删除该文件
    删除鼠标右键时“保存至360云盘”
    CSS基础知识——选择器
  • 原文地址:https://www.cnblogs.com/liuyang-520/p/15236119.html
Copyright © 2020-2023  润新知