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);
        }
    }
  • 相关阅读:
    BZOJ 2957: 楼房重建
    那些年犯下的逗比错误
    BZOJ 2165: 大楼
    BZOJ 2115: [Wc2011] Xor
    bzoj 2006 [NOI2010]超级钢琴——ST表+堆
    bzoj 4571 [Scoi2016]美味——主席树
    bzoj 1014 [JSOI2008]火星人prefix——splay+哈希
    bzoj 2962 序列操作——线段树(卷积?)
    CF 809D Hitchhiking in the Baltic States——splay+dp
    bzoj 3489 A simple rmq problem——主席树套线段树
  • 原文地址:https://www.cnblogs.com/ssxblog/p/11233436.html
Copyright © 2011-2022 走看看