常用的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();
        }
    }

常用的IO流的更多相关文章

  1. java操作文件常用的 IO流对象

    1.描述:流是字节数据或字符数据序列.Java采用输入流对象和输出流对象来支持程序对数据的输入和输出.输入流对象提供了数据从源点流向程序的管道,程序可以从输入流对象读取数据:输出流对象提供了数据从程序 ...

  2. 16个常用IO流

    在包java.io.*:下 有以下16个常用的io流类: (Stream结尾的是字节流,是万能流,通常的视频,声音,图片等2进制文件, Reader/Writer结尾的是字符流,字符流适合读取纯文本文 ...

  3. JavaSE(十二)之IO流的字节流(一)

    前面我们学习的了多线程,今天开始要学习IO流了,java中IO流的知识非常重要.但是其实并不难,因为他们都有固定的套路. 一.流的概念     流是个抽象的概念,是对输入输出设备的抽象,Java程序中 ...

  4. JAVA中IO流总结

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42119261 我想你对JAVA的IO流有所了解,平时使用的 ...

  5. 我爱Java系列之《JavaEE面试宝典》---【IO流面试总结】

    1.什么是比特(Bit),什么是字节(Byte),什么是字符(Char),它们长度是多少,各有什么区别 答案 Bit最小的二进制单位 ,是计算机的操作部分 取值0或者1 Byte是计算机操作数据的最小 ...

  6. Java学习之路(十二):IO流

    IO流的概述及其分类 IO流用来处理设备之间的数据传输,Java对数据的操作是通过流的方式 Java用于操作流的类都在IO包中 流按流向分为两种:输入流(读写数据)     输出流(写数据) 流按操作 ...

  7. IO流详解

    目录 IO流 IO流概述及其分类 IO概念 流按流向分为两种: 流按操作类型分为两种: 常用的IO流类 字节流的抽象父类: 字符流的抽象父类: InputStream & FileInputS ...

  8. java IO流的API

    常用的IO流API有:[InputStream.OutputStream] [FileInputStream.FileOutputStream] [BufferedInputStream.Buffer ...

  9. Java IO流体系中常用的流分类

    Java输入/输出流体系中常用的流分类(表内容来自java疯狂讲义) 注:下表中带下划线的是抽象类,不能创建对象.粗体部分是节点流,其他就是常用的处理流. 流分类 使用分类 字节输入流 字节输出流 字 ...

随机推荐

  1. C# 集合性能比较(代码测试)

    using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...

  2. Linux系统编程(15)——shell脚本语法

    Shell字符串 字符串是shell编程中最常用最有用的数据类型(除了数字和字符串,也没啥其它类型好用了),字符串可以用单引号,也可以用双引号,也可以不用引号.单双引号的区别跟PHP类似. 单引号 s ...

  3. UESTC_邱老师的脑残粉 2015 UESTC Training for Graph Theory<Problem D>

    D - 邱老师的脑残粉 Time Limit: 12000/4000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Sub ...

  4. Best Time to Buy and Sell Stock III 解答

    Question Say you have an array for which the ith element is the price of a given stock on day i. Des ...

  5. 算法中的增长率(Rate of Growth)是什么意思?

    一个函数或算法的代码块花费的时间随输入增长的速率称为增长率. 假设你去买一辆小车和一辆自行车.如果你朋友刚好看到,问你在买什么,我们一般都会说:买小车.因为买小车比买自行车花费高多了. [总花费=小车 ...

  6. Unity屏幕射线碰撞

    Layers层: 从Layers设置看来,最多支持32层.  图层的值开始依次 0^2,1^2, 2^2, 3^3 依次增加. 当摄像机Culling Mask属性 Nothing= –1 Eveni ...

  7. ComponentName的意思

    ComponentName是用来打开其它应用程序中的Activity或服务的. 使用方法: Intent i=new Intent(); i.setComponent(new ComponentNam ...

  8. ZendStudio快捷键 注释的快捷键

    注:本文省略“通用快捷键”描述,诸如:ctrl+N=新建,ctrl+O=打开,ctrl+C=复制,ctrl+V,ctrl+X……等等几乎所有软件都通用的一组快捷键,而着重介绍zde独有的快捷键,了解并 ...

  9. Mongodb实用网址记录

    ISODate类型算出时间戳> ISODate("2012-04-16T16:00:00Z").valueOf() 1334592000000 然后根据得到的时间戳查询即可d ...

  10. 转JS技巧

    前端已经被玩儿坏了!像console.log()可以向控制台输出图片等炫酷的玩意已经不是什么新闻了,像用||操作符给变量赋默认值也是人尽皆知的旧闻了,今天看到Quora上一个帖子,瞬间又GET了好多前 ...