常用的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. Keil中如何消除UNCALLED SEGMENT,IGNORED FOR OVERLAY PROCESS警告

    在Keil C中,如果没有显式调用到定义过的函数,就会出现这样的的警告.当出现这样的警告时,可以不用管,因为不影响其它部分.但是,我们知道,即使没有调用这个函数,Keil仍然把它编译连接进整个程序,不 ...

  2. git与svn的使用比较

    先说下基础知识: git是本地会(维护)有个版本仓库. svn本地也会维护一个自己的信息(一般是目录结构和文件状态的信息),这里的文件状态一般是指:文件是已删除,还是已添加,还是被修改等等.一般是会有 ...

  3. tk资料

    Hello World: 让我们开始,作为其他教程的开始, 以"Hello World"程序创建一个文件 叫做Hello.pl  键入下面的内容到它这里: #!/usr/local ...

  4. 使用 ServKit(PHPnow) 搭建 PHP 环境[图]

    http://servkit.org/guide 搭建 PHP 其实不很难,只是有点繁琐.要是自己搭建一次 PHP + MySQL 环境很是费时.更糟的是,很多新手在配置 PHP 时常常出现这样那样的 ...

  5. lowerCaseTableNames

    数据库表,数据库名大小写铭感问题 mysql lower-case-table-names参数 线上有业务用到开源的产品,其中SQL语句是大小写混合的,而建表语句都是小写的,mysql默认设置导致这些 ...

  6. No module named MYSQLdb 问题解决

    问题描述: 报错:ImportError: No module named MySQLdb 对于不同的系统和程序有如下的解决方法: easy_install mysql-python (mix os) ...

  7. DataReader、Table、DataSet和Entity相互转化

    public class CommonService { #region DataReader转化 /// <summary> /// 将DataReader转化为Table /// &l ...

  8. 让你的javascript函数拥有记忆功能,降低全局变量的使用

    考虑例如以下场景:假如我们须要在界面上画一个圆,初始的时候界面是空白的.当鼠标移动的时候,圆须要尾随鼠标移动.鼠标的当前位置就是圆心.我们的实现方案是:假设界面上还没有画圆,那么就新创建一个:假设已经 ...

  9. MediaPlayer本地播放流程解析(一)

    应用场景: MediaPlayer mediaPlayer = new MediaPlayer(); mediaPlayer.setOnCompletionListener(new OnComplet ...

  10. tail

    tail用于显示指定文件末尾内容,不指定文件时,作为输入信息进行处理.常用查看日志文件. -f 循环读取 -q 不显示处理信息 -v 显示详细的处理信息 -c<数目> 显示的字节数 -n& ...