• java 标准输入输出流,打印流,数据流


    1

    package stream;
    
    import static org.junit.Assert.assertNotNull;
    
    import java.io.BufferedReader;
    import java.io.DataOutput;
    import java.io.DataOutputStream;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintStream;
    
    import org.junit.jupiter.api.Test;
    
    /*
     * 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 {
                FileOutputStream fos = new FileOutputStream(new File("hello.txt"));
                //把标准输出流从 cmd 改为 文件流fos
                ps = new PrintStream(fos,true);
                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();
                }
            }
        }
        
        /*
         *     数据流: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();
                }
            }
        }
    }
  • 相关阅读:
    初探CSS
    初探CSS -3 语法
    初探 -1 JavaScript
    初探html-17 表单
    初探html-9 链接
    python 发送邮件
    检查xml文件
    python文件和目录操作方法大全(含实例)
    使用ADB无线连接Android真机进行调试
    安卓APP应用在各大应用市场上架方法整理
  • 原文地址:https://www.cnblogs.com/zsben991126/p/11880420.html
Copyright © 2020-2023  润新知