常用的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. 新发现一个函数:GradientFill

    位于Msimg32.dll之中 https://msdn.microsoft.com/en-us/library/windows/desktop/dd144957(v=vs.85).aspx

  2. 用JSON 和 Google 实现全文翻译

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  3. ###Git 基础图解、分支图解、全面教程、常用命令###

    一.Git 基础图解 转自:http://www.cnblogs.com/yaozhongxiao/p/3811130.html Git 图解剖析 git中文件内容并没有真正存储在索引(.git/in ...

  4. Activiti 5.16用户手册

    From :http://www.mossle.com/docs/activiti/ Table of Contents 1. 简介 协议 下载 源码 必要的软件 JDK 6+ Eclipse Ind ...

  5. C# StreamReader/StreamWriter与FileStream用法详解

    http://www.360doc.com/content/13/0610/08/12579422_291878973.shtml

  6. myeclipse集成weblogicserver

    今天为了学一下JMS的东东, 不得不安装个weblogicserver, 下面是详细的安装步骤: 1. 首先去官网下载一个weblogic: 下载地址: http://download.oracle. ...

  7. 提高你的Java代码质量吧:小心switch带来的空值异常

    一.分析  使用枚举定义常量时,会有伴有大量的switch语句判断,目的是为每个枚举解释其行为. 我们知道,目前的Java的switch语句只能判断byte.short.char.int类型(JDK7 ...

  8. XML.ObjTree -- XML source code from/to JavaScript object like E4X

    转载于:http://www.kawa.net/works/js/xml/objtree-try-e.html // ========================================= ...

  9. iOS控制器的创建方式

    iOS控制器的创建.除了常见的alloc init外还有通过加载storyboard和xib的方式,下边逐一展开: 1.代码alloc init 创建方式 ViewController *vc= [[ ...

  10. http://www.sufeinet.com/thread-655-1-1.html

    http://www.sufeinet.com/thread-655-1-1.html