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. Java 算法

    1.简单的冒泡排序 //冒泡算法一共两种 // -----冒泡算法(1) int a[]={23,69,4,58,1,20}; for (int i = 0; i < a.length-1; i ...

  2. 仿bmfn 底部

    <!DOCTYPE html> <!-- saved from url=(0019)http://www.bmfn.cn/ --> <html class="k ...

  3. Windows下PHP(Thread Safe与Non Thread Safe)版本说明

    转载“http://www.taoz11.com/archives/300.html” linux下直接下载源码,在服务器上编译即可,发现windows下有4个版本: VC9 x86 Non Thre ...

  4. DLNA

    DLNA最早由索尼.英特尔.微软等提出,全称是DIGITAL LIVING NETWORK ALLIANCE,旨在解决个人PC,消费电器,移动设备在内的无线网络和有线网络的互联互通,其实就是解决电脑和 ...

  5. BP神经网络代码

    close allclear allclcload x.txt; load y.txt; inputs = x';targets = y; % 创建一个模式识别网络(两层BP网络),同时给出中间层神经 ...

  6. android activity中监听View测量完成的4种方式

    在开发中经常碰到需要在activity初始化完成后获得控件大小的情况. 但是这个操作我们不能在oncreate.onresume等生命周期方法中调用,因为我们不知道何时view才能初始化完成 为此,特 ...

  7. windows任务计划程序路径设置

    用任务计划启动程序,特别是脚本,比如我要启动python脚本,其中有一句是这么写的 BasePath = removeLastSlash(os.path.abspath("..\\..\\& ...

  8. Redis简介三

    目录 一.Key 二.String 三.Hash 四.List 五.Set 六.SortedSet 七.Pub/Sub 八.Transaction 九.Script 十.Connection 十一.S ...

  9. 2013最新Android常用的工具类整理

    主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils. Pref ...

  10. C# 除法的细节

    最近在做项目时有个地方用到了概率,要计算概率自然会用到除法.我这边概率的算法是这样的,从0到10000获取个随机数,随机值除以10000就是概率了,但是一时大意没注意细节,结果直接除了,如下: 查询结 ...