深入解析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 ...
随机推荐
- POJ 2083 Fractal 分形
去年校赛团队赛就有一道分形让所有大一新生欲生欲死…… 当时就想学了 结果一直拖到…… 今天上午…… 马上要省选了 才会一点基础分形…… 还是自己不够努力啊…… 分形主要是要找到递归点…… 还有深度…… ...
- KeyTweak(笔记本键盘设置工具) V2.20 中文版
软件名称: KeyTweak(笔记本键盘设置工具)软件语言: 简体中文授权方式: 免费软件运行环境: Win 32位/64位软件大小: 50KB图片预览: 软件简介:KeyTweak 通过设置系统的注 ...
- matlab里的nargin
nargin是用来判断输入变量个数的函数,这样就可以针对不同的情况执行不同的功能.
- 批量去除office超链接
mac下: fn+shift+comman+F9 windows下:control+shift+F9
- redis事务、管道及消息通知探究
一.事务 redis中使用事务,multi表示事务开始,对redis进行一些列操作之后再用exec提交事务,对应的方法分别是Transaction jedis.multi(),List<Obje ...
- 完美版cookie设置/得到/删除2016/423
function setCookie(key, value, t) { var oDate = new Date(); oDate.setDate( oDate.getDate() + t ); do ...
- android之ViewStub的使用
转自http://blog.csdn.net/hitlion2008/article/details/6737537 在开发应用程序的时候,经常会遇到这样的情况,会在运行时动态根据条件来决定显示哪个V ...
- Android底部导航栏创建——ViewPager + RadioGroup
原创文章,引用请注明出处:http://www.cnblogs.com/baipengzhan/p/6270201.html Android底部导航栏有多种实现方式,本文详解其中的ViewPager ...
- IP子网掩码划分及设置
IP子网掩码划分及设置 定长子网掩码: 一.子网掩码的计算 TCP/IP网间网技术产生于大型主流机环境中,它能发展到今天的规模是当初的设计者们始料未及的.网间网规模的迅速扩展对IP地址模式的威胁并不是 ...
- flash Ane
1.ANE简介 Adobe AIR Native Extension,Adobe AIR的本地扩展,简称ANE.什么叫本地扩展?因为Adobe AIR是跨平台的一个运行时,可以在Windows,Mac ...