深入解析FileInputStream和FileOutputStream
2015-12-04 20:51
816 查看
http://swiftlet.net/archives/1363
FileInputStream和FileOutputStream类属于字节类,可以操作任意类型的文件。在数据流的处理过程中,有两种情况。
(1)以单个字节的形式读写文件
(2)以数据块的形式读写文件
从JDK的源码中,我们可以看出来:
FileInputStream的读:
FileOutputStream的写:
FileInputStream和FileOutputStream最常用的地方也就是文件的复制过程。下面通过两个例子来说明一下:
例子一:单个字节的读写
例子二:数据块的读写
疑问:
上面列出了两个例子,到底哪个例子的效率更高一些呢?
也许这个问题很难回答,因为效率的比对很难实现。而且数据流的底层实现,我们也很难搞清楚。其实,撇开这个问题,我们还有更高效的读取方式,那就是下一篇文章要讲的BufferedInputStream和BufferedOutputStream。
需要注意的是:
创建FileInputStream实例对象时,指定的文件应当是存在和可读的。创建FileOutputStream实例对象时,如果指定的文件已经存在,这个文件中的原来内容将被覆盖清除。
声明: 本文由金丝燕网原创编译,转载请保留链接: 深入解析FileInputStream和FileOutputStream
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; |
1 2 | public native void write(int b) throws IOException; private native void writeBytes(byte b[], int off, int len) throws IOException; |
例子一:单个字节的读写
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
相关文章推荐
- Redhat7配置vsftpd
- LTP学习
- PHP图片等比缩放,并添加Logo水印特定代码和盯
- PHP 7.0.0 正式版发布!
- 安装PHP7
- YII2-RBAC-Plus-安装篇
- zabbix 之 PHP
- php文件上传
- ThinkPHP框架六数据的添加
- Windows server 2008系统下FTP服务器的安装
- php rsa加密
- crontab curl php实现定时任务
- ThinkPHP框架--关联查询--HAS_MANY--小demo
- zend studio 改主题
- php7 安装 2
- php7 安装 (1)
- yii2 嵌套布局 layout
- Php数组
- Yii2 前端资源使用
- php内联式写法