java基础——标准输入输出重定向,数据流

感觉这两个流挺实用的

第一个是重定system.in/out的

/*
 * 1.标准输入输出流
 *         System.in :标准的输入流,默认从键盘输入
 *         System.out  标准的输出流,默认从控制台输出
 *     System类的setIn(InputStream is) / setOut(PrintStream ps)方式重新指定
 *     
 * */

public class OtherStreamTest {
    /*
     *     方法一:使用Scanner实现,调用next()方法即可
     *     方法二:使用System.in实现读入,System.in -> 转换流  ->  BufferedReader的readline()
     *     
     * */
    @Test
    public void test1() {
        //得到标准输入流
        BufferedReader br = null;
        try {
            //System.in的流是字节流,所以要转换成字符流
            InputStreamReader isr = new InputStreamReader(System.in);
            //再将字符流转包装为缓冲流
            br = new BufferedReader(isr);
            
            while(true) {
                String data = br.readLine();//直接读入一行
                if("e".equalsIgnoreCase(data) || "exit".equalsIgnoreCase(data)) {
                    System.out.println("程序结束");
                    break;
                }
                String upperString = data.toUpperCase();
                System.out.println(upperString);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(br!=null)
                    br.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    
    /*打印流
     *         字节输出流PrintStream
     *         字符输出流PrintWriter
     *     提供一系列重载的print()和println()
     * */
    @Test
    public void test2() throws FileNotFoundException {
        //文件hello.txt绑定输出流
        PrintStream ps = null;
        try {
            //重定向标准输出的步骤
            //1.new 字节流对象
            FileOutputStream fos = new FileOutputStream(new File("hello.txt"));
            //2.用字节流来 new 字节输出流ps,这个流就为打印流
            ps = new PrintStream(fos,true);
            //3.将标准输出从cmd重定向到打印流ps
            if(ps!=null)
                System.setOut(ps);
            
            
            
            for(int i=0;i<255;i++) {
                System.out.print((char)i);
                if(i % 50 == 0)
                    System.out.println();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(ps!=null)
                    ps.close();
            } catch (Exception e) { 
                e.printStackTrace();
            }
        }
    }
    

第二个流用来从文件流里读/写Int等数据

/*
     *     数据流:DataInputStream:套在InputStream和OutputStream上面
     *     作用:用于读取或写入基本数据类型的变量或字符串
     *         
     * */
    @Test
    public void test3()  {
        DataOutputStream dos = null;
        try {
            //将内存中的字符串,基本数据类型的变量写到文件中
            dos = new DataOutputStream(new FileOutputStream(new File("hello.txt")));
            dos.writeUTF("zsben");
            dos.flush();
            dos.writeInt(23);
            dos.writeBoolean(true);
            dos.flush();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally {
            try {
                if(dos!=null)
                    dos.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/zsben991126/p/12161118.html