zoukankan      html  css  js  c++  java
  • 打印流

    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();
                }
            }
        }
    }
    相识是缘
  • 相关阅读:
    JS中算法之排序算法
    JS中数据结构之图
    JS中数据结构之二叉查找树
    JS中数据结构之集合
    JS中数据结构之散列表
    JS中生成随机数
    JS中数据结构之字典
    JS中数据结构之链表
    JS中数据结构之队列
    JS中数据结构之栈
  • 原文地址:https://www.cnblogs.com/liuyang-520/p/15236119.html
Copyright © 2011-2022 走看看