zoukankan      html  css  js  c++  java
  • 工具类关闭流及打印流

    工具类关闭流(close)

    package cn.Buffered;
    
    import java.io.Closeable;
    import java.io.IOException;
    
    public class FileUtil {
    /*
     * 工具类关闭流
     * 可变参数:... 只能形参最后一个位置,处理方式与数组一致
     * 
     */
        public static void close(Closeable ... io) {
            for(Closeable temp:io) {
                if(null!=temp) {
                    try {
                        temp.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
        /*
         * 使用泛型方法
         */
        public static <T extends Closeable> void closrAll(T ... io) {
            for(Closeable temp:io) {
                if(null!=temp) {
                    try {
                        temp.close();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    打印流(打印到文本,而不是控制台)

    package cn.Buffered;
    
    import java.io.BufferedOutputStream;
    import java.io.FileDescriptor;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.PrintStream;
    
    public class SystemDemo01 {
        public static void main(String[] args) throws FileNotFoundException {
            //重定向
            System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream("C:/Users/Administrator/Desktop/sun/a.txt")),true));
            System.out.println("a");    //控制台 --文件
            System.out.println("sssa");
            //返回控制台打印
            System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(FileDescriptor.out)),true));
            System.out.println("sadafdfda");
        }
    }

    封装输入流(类似于Scanner)

    package cn.Buffered;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    public class Scanner {
        public static void main(String[] args) throws IOException {
            InputStream is= System.in;
            BufferedReader br=new BufferedReader(new InputStreamReader(is));
            System.out.println("请输入");
            String msg=br.readLine();
            System.out.println(msg);
        }
    }
  • 相关阅读:
    七言丨做个俗人,浪荡一生,干净自由。
    当你无法原谅父母时,那就不要原谅
    node_modules干什么的?
    流的操作(一)视频转音频引发的血案
    OpenGL ES 压缩纹理
    用 .SqlSugar ORM 来实现报表功能 .NET CORE /.NET
    Opencv 播放mp4文件和读取摄像头图以及可能会发生的一些异常问题解决方法
    Vue 前端权限控制的优化改进版
    输入框占位符placeholder
    为DOM节点添加或者删除class
  • 原文地址:https://www.cnblogs.com/ssxblog/p/11233436.html
Copyright © 2011-2022 走看看