Java IO流------标准输入输出流、打印流和数据流总结

标准输入输出流

  • 标准输入、输出流也是处理流之一
  • System.in和System.out分别代表了系统标准的输入和输出设备
  • 默认输入设备:键盘;输出设备:显示器(控制台)
  • System类的setIn(InputStream is)/ setOut(printStream ps)方式重新指定输入和输出
/*
从键盘输入字符串,要求将读取到的整行字符串转成大写输出,然后继续进行输入操作,直至当输入“e”或者“exit”时,退出程序
方法一:使用Scanner实现,调用next()返回一个字符串
方法二:使用System.in实现,System.in --->转换流--->BufferedReader的readLine()
*/
package www.bh.c.iotest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Test14 {
    
    
    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 s = br.readLine();
                if ("e".equalsIgnoreCase(s)||"exit".equalsIgnoreCase(s)){
    
    
                    System.out.println("程序结束");
                    break;
                }
                //将读取到的整行字符串转成大写输出
                String s1 = s.toUpperCase();
                System.out.println(s1);
            }
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            //关闭流资源
            if (br!=null){
    
    
                try {
    
    
                    br.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 总结:
  • 输入过程:
    • 1.创建File类的对象,指明读取的数据的来源(要求此文件一定要存在)
    • 2.创建相应的输入流,将File类的对象作为参数,传入流的构造器中
    • 3.具体的读入过程:创建相应的byte[] 或char[]
    • 4.关闭流资源
  • 输出过程:
    • 1.创建File类的对象,指明写出的数据的位置(不要求此文件一定要存在)
    • 2.创建相应的输出流,将File类的对象作为参数,传入流的构造器中
    • 3.具体的写出过程:write(char[]/byte[] buffer,0,len)
    • 4.关闭流资源

打印流

  • 打印流也是处理流的一种,实现将基本数据类型的数据格式转化为字符串输出
  • PrintStream()和PrintWriter()
  • 提供了一系列重载的print()和printIn()

数据流

  • 打印流也是处理流的一种,用于读取和写出基本数据类型、String类的数据
  • DataInputStream和DataOutputStream
  • 以上两个类分别“套接”在InputStream和OutputStream子类的流上
//将内存中的字符串、基本数据类型的变量写到文件中
package www.bh.c.iotest;
import java.io.DataOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Test15 {
    
    
    public static void main(String[] args) {
    
    
        DataOutputStream dos = null;
        try {
    
    
            dos = new DataOutputStream(new FileOutputStream("calss\\hello1.txt",true));
            dos.writeUTF("Java");
            dos.flush();//刷新操作,将内存中的数据写入文件
            dos.writeInt(18);
            dos.flush();
            dos.writeBoolean(true);
            dos.flush();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
            if (dos!=null){
    
    
                try {
    
    
                    dos.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}
//将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中
package www.bh.c.iotest;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class Test16 {
    
    
    public static void main(String[] args) {
    
    
        DataInputStream dis = null;
        try {
    
    
             //创建流
            dis = new DataInputStream(new FileInputStream("calss\\hello1.txt"));
           //将文件中存储的基本数据类型变量和字符串读取到内存中,保存在变量中
            String s=dis.readUTF();
            int age=dis.readInt();
            boolean isMale=dis.readBoolean();
            System.out.println("name:"+s);
            System.out.println("age:"+age);
            System.out.println("isMale:"+isMale);
        } catch (IOException e) {
    
    
            e.printStackTrace();
        } finally {
    
    
             //关闭流
            if (dis!=null){
    
    
                try {
    
    
                    dis.close();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        }
    }
}
//注:读取不同类型的数据的顺序要与当初写入文件时,保存的数据顺序一致

猜你喜欢

转载自blog.csdn.net/insist_to_learn/article/details/110560742