JavaIO流-其他流的使用

import org.junit.Test;

import java.io.*;

/**
 * 其他流的使用
 * 一、标准输入输出流
 * //标准输入、输出流
 * //System.in:标准的输入流,默认键盘输入
 * //System.out:标准的输出流,默认从控制台输出
 * 二、打印流
 * 都是输出流
 * PrintStream\PrintWriter
 * 三、数据流
 *DataInputStream
 *DataOutputStream
 * @author orz
 */
public class orderTest {
    @Test
    public void test1()
    {
        //标准输入、输出流
        //System.in:标准的输入流,默认键盘输入
        //System.out:标准的输出流,默认从控制台输出
        //练习
        /**
         * 从键盘输入字符串,要求将读取到的整行字符串转换成大写输出。然后继续进行输入操作,
         *直到当输入“e”或者“exit”时,退出程序。
         * 方法一、使用Scanner实现,调用next()返回一个字符串
         * 方法二:使用System.in实现,System.in  -->转换流  ---->BufferedReader的readLine()
         */
        BufferedReader br=null;
        try
        {
            InputStreamReader isr =new InputStreamReader(System.in);
            br=new BufferedReader(isr);
            while (true)
            {
                System.out.println("请输入字符串:");
                String data=br.readLine();
                if("e".equalsIgnoreCase(data)|| "exit".equalsIgnoreCase(data))
                {
                    System.out.println("程序结束");
                    break;
                }
                String upperCase=data.toUpperCase();
                System.out.println(upperCase);
            }
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally {
            try {
                if(br!=null)
                {
                    br.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }


    }

    /**
     * 打印流:PrintStream  PrintWriter
     *
     */
    @Test
    public void test2()
    {
        PrintStream ps=null;
        FileOutputStream  fos= null;
        try {
            //创建打印输出流,设置为自动刷新模式(写入换行符或者字节“\n”时都会刷新输出缓冲区)
            fos = new FileOutputStream(new File("E:\\test.txt"));
            ps=new PrintStream(fos,true);
            if(ps!=null)
            {
                //把标准输出流(控制台输出)改为文件
                System.setOut(ps);

            }
            for (int i = 0; i <255 ; i++) {
                //输出ASCII字符
                System.out.print((char)i);
                if(i%20==0)
                {
                    //每20个数据换行
                    System.out.println();
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {

            if (ps != null) {
                ps.close();
            }
        }


    }



    public static void main(String[] args) {
//        orderTest ot=new orderTest();
//        ot.test1();
    }


    @Test
    public void test3()throws  IOException
    {
        //DataInputStream
        //DataOutputStream
        //作用:用于读取或写出基本数据类型的变量或字符串
        //将内存中基本数据类型变量和字符串读取到硬盘中,保存在文件中。
        DataOutputStream dos=new DataOutputStream(new FileOutputStream("data.txt") );
        dos.writeUTF("刘晨");
        dos.flush();
        dos.writeInt(23);
        dos.flush();
        dos.close();
    }

    /**
     * 将文件存储的基本数据类型变量和字符串读取到内存中,保存在变量中。
     * 注意:读取不同类型的数据的顺序要和当初写入文件时保存的数据顺序一致
     *
     */
    @Test
    public void test4()throws  IOException
    {
        DataInputStream dis=new DataInputStream(new FileInputStream("data.txt"));
        String name=dis.readUTF();
        int age=dis.readInt();

        System.out.println("name="+name);
        System.out.println("age"+age);
        dis.close();
    }

}

 

posted @ 2020-07-29 18:01  orz江小鱼  阅读(99)  评论(0编辑  收藏  举报