常用的IO流

常用的IO流

•根据处理数据类型的不同分为:字节流和字符流

•根据数据流向不同分为:输入流和输出流

字节流:
字节流以字节(8bit)为单位,能处理所有类型的数据(如图片、avi等)。

字节输入流:
InputStream 是所有的输入字节流的父类,它是一个抽象类。

常用的字节输入流:
ByteArrayInputStream、StringBufferInputStream、FileInputStream、PipedInputStream,
它们分别从Byte 数组、StringBuffer、本地文件中、和从与其它线程共用的管道中读取数据。
ObjectInputStream 和所有FilterInputStream 的子类都是装饰流。

字节输出流:
OutputStream 是所有的输出字节流的父类,它是一个抽象类。

常用的字节输出流:
2.ByteArrayOutputStream、FileOutputStream、PipedOutputStream,它们分别向Byte 数组、本地文件、和向与其它线程共用的管道中写入数据。
ObjectOutputStream 和所有FilterOutputStream 的子类都是装饰流。

字符流:
只能处理字符类型的数据,因为数据编码的不同,而有了对字符进行高效操作的流对象。
本质其实就是基于字节流读取时,去查了指定的码表。读写以字符为单位,一次可能读多个字节。

字符输入流:
Reader 是所有的输入字符流的父类,它是一个抽象类。

常见的字符输入流:
CharReader、StringReader、PipedReader
InputStreamReader 是一个连接字节流和字符流的桥梁,可对读取到的字节数据经过指定编码转换成字符。
FileReader 可以说是一个达到此功能、常用的工具类
BufferedReader 是一个装饰器,它和其子类负责装饰其它Reader 对象。
FilterReader 是所有自定义具体装饰流的父类

字符输出流
Writer 是所有的输出字符流的父类,它是一个抽象类。

常见的字符输出流:
CharArrayWriter、StringWriter、PipedWriter、PrintWriter
OutputStreamWriter 是OutputStream 到Writer 转换的桥梁,可对读取到的字符数据经过指定编码转换成字节。
FileWriter 可以说是一个达到此功能、常用的工具类
BufferedWriter 是一个装饰器为Writer 提供缓冲功能。

字节流的例子:

 1     /**
 2      * @ 字节流
 3      */
 4     public static void streamDemo() {
 5         try {
 6             // 要读入的文件
 7             File inputFile = new File("d:\book.txt");
 8             // 文件不存在的话,什么也不做
 9             if (!inputFile.exists()) {
10                 System.out.println("file is not exists!");
11                 return;
12             }
13             // 读入流
14             InputStream in = new FileInputStream(inputFile);
15             // 输出流
16             OutputStream out = new FileOutputStream(new File("d:\book" + System.currentTimeMillis() + ".txt"));
17             // 读到的字节数
18             int readBytes;
19             byte [] buffer = new byte [1024];
20             while ((readBytes = in.read(buffer)) != -1) {
21                 out.write(buffer,0,readBytes);
22             }
23             out.flush();
24             out.close();
25             in.close();
26         } catch (IOException e) {
27             e.printStackTrace();
28         }
29     }

字符流的例子:

 1     /**
 2      * @ 字符流
 3      */
 4     public static void readerDemo() {
 5         try {
 6             // 要读入的文件
 7             File inputFile = new File("d:\book.txt");
 8             // 文件不存在的话,什么也不做
 9             if (!inputFile.exists()) {
10                 System.out.println("file is not exists!");
11                 return;
12             }
13             // 读入流
14             InputStreamReader in = new FileReader(inputFile);
15             BufferedReader bf = new BufferedReader(in);
16             // 输出流
17             PrintWriter out = new PrintWriter(new File("d:\book" + System.currentTimeMillis() + ".txt"));
18             String str;
19             while ((str = bf.readLine()) != null) {
20                 System.out.println(str);
21                 out.println(str);
22             }
23             out.flush();
24             out.close();
25             bf.close();
26             
27         } catch (IOException e) {
28             e.printStackTrace();
29         }
30     }

编码转换的例子:

 1     /**
 2      * @ 编码转换
 3      */
 4     public static void gbk2utf8Demo() {
 5         try {
 6             // 要读入的文件
 7             File inputFile = new File("d:\book.txt");
 8             // 文件不存在的话,什么也不做
 9             if (!inputFile.exists()) {
10                 System.out.println("file is not exists!");
11                 return;
12             }
13             // 读入时文件编码为gbk
14             InputStreamReader in = new InputStreamReader(new FileInputStream(inputFile),"gbk");
15             BufferedReader bf = new BufferedReader(in);
16             // 输出时文件编码变为utf-8
17             OutputStreamWriter outwriter = 
                new OutputStreamWriter(new FileOutputStream(new File("d:\book" + System.currentTimeMillis() + ".txt")),"utf-8");
18             PrintWriter out = new PrintWriter(outwriter,true);
19             String str;
20             // 一直读到文件的最后为止
21             while ((str = bf.readLine()) != null) {
22                 System.out.println(str);
23                 out.println(str);
24             }
25             // 关闭流
26             out.flush();
27             out.close();
28             bf.close();
29             
30         } catch (IOException e) {
31             e.printStackTrace();
32         }
33     }

对象序列化与反序列化的例子:

class User implements Serializable{
    String user_id;
    String user_nm;
    int age;
}

    /**
     * @ 对象的序列化与反序列化
     
*/
    public static void serializeDemo() {
        try {
            
            // 输出流的定义
            File file = new File("d:\book" + System.currentTimeMillis() + ".dat");
            ObjectOutputStream ob = new ObjectOutputStream(new FileOutputStream(file));
            // 输出内容设定
            User user1 = new User();
            user1.user_id = "110";
            user1.user_nm = "police";
            user1.age = 25;
            // 输出序列化对象
            ob.writeObject(user1);
            // 关闭输出流
            ob.close();
            
            // 打开输入流
            ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
            // 读入序列化对象
            User user = (User)in.readObject();
            System.out.println("userid: " + user.user_id);
            System.out.println("username: " + user.user_nm);
            System.out.println("age: " + user.age);
            // 关闭输入流
            in.close();
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

读写特定类型数据的例子:

    /**
     * @ 读写特定数据类型的流
     
*/
    public static void dataOutputStreamDemo () {
        try {
            // 输出流
            File file = new File("d:\data.dat");
            FileOutputStream os = new FileOutputStream(file,false);
            DataOutputStream out = new DataOutputStream(os);
            // 布尔型
            boolean val0 = true;
            // 整型
            int val1 = 110;
            // 浮点型
            double val2 = 110.011;
            
            // 输出
            out.writeBoolean(val0);
            out.writeInt(val1);
            out.writeDouble(val2);
            
            // 关闭输出流
            out.flush();
            out.close();
            
            // 输入流
            FileInputStream is = new FileInputStream(file);
            DataInputStream in = new DataInputStream(is);
            // 打印结果
            System.out.println(in.readBoolean());
            System.out.println(in.readInt());
            System.out.println(in.readDouble());
            // 关闭输入流
            in.close();
            is.close();
            
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
原文地址:https://www.cnblogs.com/keyiei/p/3588821.html