http://swiftlet.net/archives/1363

FileInputStream和FileOutputStream类属于字节类,可以操作任意类型的文件。在数据流的处理过程中,有两种情况。
(1)以单个字节的形式读写文件
(2)以数据块的形式读写文件
从JDK的源码中,我们可以看出来:
FileInputStream的读:

 
1
2
public native int read() throws IOException;
private native int readBytes(byte b[], int off, int len) throws IOException;

FileOutputStream的写:

 
1
2
public native void write(int b) throws IOException;
private native void writeBytes(byte b[], int off, int len) throws IOException;

FileInputStream和FileOutputStream最常用的地方也就是文件的复制过程。下面通过两个例子来说明一下:
例子一:单个字节的读写

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
public class Test
{
    public static void main(String[] args)
    {
        File src = new File("d:\\src.txt");
        File dst = new File("d:\\dst.txt");
        doSaveFile(src, dst);
    }
    public static void doSaveFile(File src, File dst)
    {
        InputStream in = null;
        OutputStream out = null;
        try
        {
            in = new FileInputStream(src);
            out = new FileOutputStream(dst);
            int len = 0;
            while ((len = in.read()) > 0)
            {//len表示读取的字节
                out.write(len);
            }
        }
        catch (Exception e)
        {
        }
        finally
        {
            if (null != in)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                }
            }
            if (null != out)
            {
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                }
            }
        }
    }
}

例子二:数据块的读写

 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
public class Test
{
    private final static int BUFFER_SIZE = 16 * 1024;
    public static void main(String[] args)
    {
        File src = new File("d:\\src.txt");
        File dst = new File("d:\\dst.txt");
        doSaveFile(src, dst);
    }
    public static void doSaveFile(File src, File dst)
    {
        InputStream in = null;
        OutputStream out = null;
        try
        {
            in = new FileInputStream(src);
            out = new FileOutputStream(dst);
            byte[] buffer = new byte[BUFFER_SIZE];
            int len = 0;
            while ((len = in.read(buffer)) > 0)
            {//len表示读取的字节数
                out.write(buffer, 0, len);
            }
        }
        catch (Exception e)
        {
        }
        finally
        {
            if (null != in)
            {
                try
                {
                    in.close();
                }
                catch (IOException e)
                {
                }
            }
            if (null != out)
            {
                try
                {
                    out.close();
                }
                catch (IOException e)
                {
                }
            }
        }
    }
}

疑问:
上面列出了两个例子,到底哪个例子的效率更高一些呢?
也许这个问题很难回答,因为效率的比对很难实现。而且数据流的底层实现,我们也很难搞清楚。其实,撇开这个问题,我们还有更高效的读取方式,那就是下一篇文章要讲的BufferedInputStream和BufferedOutputStream。
需要注意的是:
创建FileInputStream实例对象时,指定的文件应当是存在和可读的。创建FileOutputStream实例对象时,如果指定的文件已经存在,这个文件中的原来内容将被覆盖清除。

 

声明: 本文由金丝燕网原创编译,转载请保留链接: 深入解析FileInputStream和FileOutputStream

深入解析FileInputStream和FileOutputStream的更多相关文章

  1. FileInputStream与FileOutputStream类 Reader类和Writer类 解析

    FileInputStream和FileOutputStream类分别用来创建磁盘文件的输入流和输出流对象,通过它们的构造函数来指定文件路径和文件名. 创建FileInputStream实例对象时,指 ...

  2. 【Java IO】FileInputStream 和 FileOutputStream

    class FileInputStream extends  InputStream implements Closeable

  3. 2016-9-6 批量给文件名的前面加上“igeek_高薪就业” 2、 利用FileInputStream和FileOutputStream复制文件

    在此只列出典型题目,有的题目扫一眼就有代码的不去浪费时间了,想要完整题目的评论留邮箱,看到就发.持续更新中... 1.批量给文件名的前面加上“igeek_高薪就业” package com.work; ...

  4. java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr

    BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWri ...

  5. FileInputStream和FileOutputStream详解

    一.引子 文件,作为常见的数据源.关于操作文件的字节流就是 FileInputStream & FileOutputStream.它们是Basic IO字节流中重要的实现类.二.FileInp ...

  6. [八]JavaIO之FileInputStream 与 FileOutputStream

    接下来介绍 FileInputStream  和 FileOutputStream 现在看名字应该可以看得出来: 他就是从一个文件中读取数据 或者将数据写入到一个文件中 FileInputStream ...

  7. java io系列07之 FileInputStream和FileOutputStream

    本章介绍FileInputStream 和 FileOutputStream 转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_07.html File ...

  8. java-IO流-字节流-概述及分类、FileInputStream、FileOutputStream、available()方法、定义小数组、BufferedInputStream、BufferedOutputStream、flush和close方法的区别、流的标准处理异常代码

    1.IO流概述及其分类 * 1.概念      * IO流用来处理设备之间的数据传输      * Java对数据的操作是通过流的方式      * Java用于操作流的类都在IO包中      *  ...

  9. FileInputstream,FileOutputstream 和 byteArrayInputStream,byteArrayOutputStream

    你知道FileInputstream和FileOutputstream吗?FileInputstream,FileOutputstream分别是由抽象类Inputstream和Outputstream ...

随机推荐

  1. php缓存总结

    php缓存技术: 1.全页面静态化缓存;2.页面部分缓存;3.数据缓存;4.查询缓存;5.按内容变更进行缓;6.内存式缓存;7.apache缓存模块;8.php APC缓存扩展;9.Opcode缓存. ...

  2. 官方解答:Vultr VPS常见问题

    VULTR VPS配置高,价格低廉,是非常优秀的vps品牌.今天我来翻译vultr官方FAQ,相信你能找到具体答案. Q 请介绍VULTR VPS机器硬件配置 Intel CPU 3+ GHz Cor ...

  3. erlang四种监控策略

    转自:http://jasionq.blog.163.com/blog/static/10970577920133883158424/ Supervisor Behaviour是一个用来实现一个sup ...

  4. kvstore存储介质redis代码

    <?php /** * ShopEx licence * * @copyright Copyright (c) 2005-2010 ShopEx Technologies Inc. (http: ...

  5. yaf for ubuntu安装

    一.安装yaf需要的扩展 apt-get install perl-modules apt-get install libpcrecpp0 apt-get install libpcre3 libpc ...

  6. java中的静态变量

    大家都知道,我们可以基于一个类创建多个该类的对象,每个对象都拥有自己的成员,互相独立.然而在某些时候,我们更希望该类所有的对象共享同一个成员.此时就是 static 大显身手的时候了!! Java 中 ...

  7. openwrt ramips随记

    ar71xx / brcm47xx / brcm63xx / ramips是指cpu的系列,ramips是指ralink系列的

  8. LeetCode OJ 61. Rotate List

    Given a list, rotate the list to the right by k places, where k is non-negative. For example:Given 1 ...

  9. World Cup

    World Cup Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Su ...

  10. Ansible8:Playbook循环【转】

    在使用ansible做自动化运维的时候,免不了的要重复执行某些操作,如:添加几个用户,创建几个MySQL用户并为之赋予权限,操作某个目录下所有文件等等.好在playbook支持循环语句,可以使得某些需 ...