深入解析FileInputStream和FileOutputStream
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的更多相关文章
- FileInputStream与FileOutputStream类 Reader类和Writer类 解析
FileInputStream和FileOutputStream类分别用来创建磁盘文件的输入流和输出流对象,通过它们的构造函数来指定文件路径和文件名. 创建FileInputStream实例对象时,指 ...
- 【Java IO】FileInputStream 和 FileOutputStream
class FileInputStream extends InputStream implements Closeable
- 2016-9-6 批量给文件名的前面加上“igeek_高薪就业” 2、 利用FileInputStream和FileOutputStream复制文件
在此只列出典型题目,有的题目扫一眼就有代码的不去浪费时间了,想要完整题目的评论留邮箱,看到就发.持续更新中... 1.批量给文件名的前面加上“igeek_高薪就业” package com.work; ...
- java中OutputStream字节流与字符流InputStreamReader 每一种基本IO流BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWriter,FileInputStream,FileReader,FileWriter,InputStr
BufferedOutputStream,FileInputStream,FileOutputStream,BufferedInputStream,BufferedReader,BufferedWri ...
- FileInputStream和FileOutputStream详解
一.引子 文件,作为常见的数据源.关于操作文件的字节流就是 FileInputStream & FileOutputStream.它们是Basic IO字节流中重要的实现类.二.FileInp ...
- [八]JavaIO之FileInputStream 与 FileOutputStream
接下来介绍 FileInputStream 和 FileOutputStream 现在看名字应该可以看得出来: 他就是从一个文件中读取数据 或者将数据写入到一个文件中 FileInputStream ...
- java io系列07之 FileInputStream和FileOutputStream
本章介绍FileInputStream 和 FileOutputStream 转载请注明出处:http://www.cnblogs.com/skywang12345/p/io_07.html File ...
- java-IO流-字节流-概述及分类、FileInputStream、FileOutputStream、available()方法、定义小数组、BufferedInputStream、BufferedOutputStream、flush和close方法的区别、流的标准处理异常代码
1.IO流概述及其分类 * 1.概念 * IO流用来处理设备之间的数据传输 * Java对数据的操作是通过流的方式 * Java用于操作流的类都在IO包中 * ...
- FileInputstream,FileOutputstream 和 byteArrayInputStream,byteArrayOutputStream
你知道FileInputstream和FileOutputstream吗?FileInputstream,FileOutputstream分别是由抽象类Inputstream和Outputstream ...
随机推荐
- Logistic Regression:银行贷款申请审批实例
问题定义 这是一个贷款的审批问题,假设你是一个银行的贷款审批员,现在有客户需要一定额度的贷款,他们填写了个人的信息(信息在datas.txt中给出),你需要根据他们的信息,建立一个分类模型,判断是否可 ...
- 2016NEFU集训第n+3场 G - Tanya and Toys
Description In Berland recently a new collection of toys went on sale. This collection consists of 1 ...
- 仿bmfn 底部
<!DOCTYPE html> <!-- saved from url=(0019)http://www.bmfn.cn/ --> <html class="k ...
- Linux下搭建ntp时间同步服务器
1.ntpd软件安装(略过) 2.修改ntp.conf配置文件 vi /etc/ntp.conf 第一种配置:允许任何IP的客户机都可以进行时间同步将“restrict default kod nom ...
- CentOS 7 安装Python pip
1 首先安装epel扩展源: sudo yum -y install epel-release 2 然后安装python-pip sudo yum -y install python-pip 3安装完 ...
- 使用HttpClient工具类测试WebService接口(soap)
import java.io.ByteArrayInputStream;import java.io.IOException;import java.io.InputStream;import jav ...
- js 数组切换图片
<html> <head> <meta charset="utf-8" /> <title></title> <s ...
- Codeforces 237C
题目: Description You've decided to carry out a survey in the theory of prime numbers. Let us remind y ...
- .NET面试题目二
1.对你来说工作中最重要的是什么? 答:团队目标的实现.(这是所有公司希望员工拥有的素质) 2.为什么愿意为本公司工作? 答:因为我认为我可以与贵公司取得共同的发展.(暗示留下我可能为公司带来的益处) ...
- CTRL key
ctrl key其实是用于扩展键盘,单独一个ctrl键没有什么作用,也没有ascii码,当与其他键相结合时,相当于创造出一个新键.例如:用getchar()侦测输入的字符,当按下ctrl+a时,只输出 ...