JavaIO流之其他一些流

一、标准的输入、输出流

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

练习:
从键盘输入字符串,要求将读取到的整行字符串转成大写输出。然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序。
方法一:使用Scanner实现,调用next()返回一个字符串
方法二:使用System.in实现。 System.in ---> 转换流 ---> BufferedReader的readLine()
方法二测试代码:

    public static void main(String[] args) {
        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 s = data.toUpperCase();
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)
                    br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

二、打印流PrintStream 和 PrintWriter

提供了一系列重载的print()和println()
练习:使用打印输出流将ASCII码打印到txt文件中
测试代码:

    @Test
    public void test1(){
        PrintStream ps = null;
        try {
            FileOutputStream fos = new FileOutputStream("ascii码.txt");
            //创建打印输出流,设置为自动刷新模式(写入换行符或字节'\n'时都会刷新输出缓冲区)
            ps = new PrintStream(fos, true);
            if (ps != null) {
                //将标准输出流改成文件输出流
                System.setOut(ps);
            }
            //输出ASCII码
            for (int i = 0; i <= 255; i++) {
                System.out.print((char) i);
                if (i % 50 == 0)
                    System.out.println();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (ps != null) {
                ps.close();
            }
        }
    }

三、数据流

DataInputStream和DataOutputStream
作用:用于读取或写出基本数据类型的变量或字符串
DataOutputStream的使用
将内存中的字符串、基本数据类型的变量写出到文件中。
测试代码:

    @Test
    public void test2(){
        DataOutputStream dos = null;
        try {
            //1
            dos = new DataOutputStream(new FileOutputStream("data.txt"));
            //2
            dos.writeUTF("zyj");
            dos.flush();//刷新操作,将内存中的数据写入文件
            dos.writeInt(23);
            dos.flush();
            dos.writeBoolean(true);
            dos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //3
            if (dos != null) {
                try {
                    dos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

DataInputStream的使用
将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中。
注意点:读取不同类型的数据的顺序要与当初写入文件时,保存的数据的顺序一致。
测试代码:

    @Test
    public void test3(){
        DataInputStream dis = null;
        try {
            //1
            dis = new DataInputStream(new FileInputStream("data.txt"));
            //2
            String name = dis.readUTF();
            int age = dis.readInt();
            boolean isMale = dis.readBoolean();
            System.out.println("name : " + name);
            System.out.println("age : " + age);
            System.out.println("isMale : " + isMale);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //3
            if (dis != null) {
                try {
                    dis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
posted @ 2021-06-29 17:48  koito  阅读(56)  评论(0)    收藏  举报