使用try catch finally关闭文件流:

  写入文件:

import java.io.*;
public class exp{
public static void main(String[] args) {
//流要在try外面声明,不然finally里面找不到这个流
OutputStream file = null;
try{
file = new FileOutputStream("iooooo.txt");
String str = "北邮\n";
byte[] b = str.getBytes();
for (int i=0; i<b.length; i++){
file.write(b[i]);
}
//在这里,如果发生异常,则close()不能被执行,所以要在finally里close()
//file.close();
}catch(Exception e){
e.printStackTrace();
}finally{
try{
if(file!=null)
file.close();
}catch(Exception e){
e.printStackTrace();
}
} }
}

  读文件:

import java.io.*;
public class exp{
public static void main(String[] args) {
//流要在try外面声明,不然finally里面找不到这个流
InputStream file = null;
try{
file = new FileInputStream("ioo.txt");
int b = 0;
while(true){
b = file.read();
if(b == -1){
break;
}
System.out.println(b);
}
//在这里,如果发生异常,则close()不能被执行,所以要在finally里close()
//file.close();
}catch(Exception e){
System.out.println("try");
}finally{
try{
if(file!=null)
file.close();
System.out.println(file);
}catch(Exception e){
System.out.println("finally");
}
} }
}

字节流缓冲区拷贝文件:

import java.io.*;
public class exp{
public static void main(String[] args) throws Exception {
InputStream in = new FileInputStream("pic.png");
OutputStream out = new FileOutputStream("pp.png");
byte[] buff = new byte[1024];
int len;
while((len=in.read(buff)) != -1){
out.write(buff, 0, len);
}
in.close();
out.close();
}
}

在写这个的时候我没有仔细研究b=in.read()和b=in.read(byte[])的区别,以至于没弄懂这个buff。下面简单说明一下:

第一种:

int len;
while((len=in.read()) != -1){
out.write(len);
}

上面这种情况下in.read()返回的是从输入流中读取的8个字节的数据,并以int形式保存在len中。out.write(len);接受int中保存的数据,并将其写入out流中。

第二种:

byte[] buff = new byte[1024];
int len;
while((len=in.read(buff)) != -1){
out.write(buff, 0, len);
}

这种情况下,len=in.read(buff);中len表示的是读取字节的数目,此例就是1024,而真正存储数据的变成了buff这个字节数组。out.write(buff, 0, len);接受buff中从0到len个字节的数据,并将其写入out流中。

bingo~

字节缓冲流:存储很快~

import java.io.*;
public class exp{
public static void main(String[] args) throws Exception {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream("pic.png"));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("pp.png"));
int b;
while((b=bis.read()) != -1){
bos.write(b);
}
bis.close();
bos.close();
}
}

最新文章

  1. [Sass]扩展/继承
  2. ubuntu中常用软件的安装
  3. JS 页面加载触发事件 document.ready和window.onload的区别
  4. Redis的发布订阅
  5. PHP 缓存扩展opcache
  6. IBM Lotus Domino V8.5 服务器管理入门手册
  7. jQuery中的遍历
  8. 如何加入自定义WebControl
  9. Web Service 之 开发、部署
  10. Ajax异步操作集合啦(阿贾克斯)
  11. 适用于 PHP 开发人员的 Python 基础知识
  12. android手机内存大小获取
  13. HTML--控制小人自由移动
  14. hdu 1811 Rank of Tetris
  15. 获取android源码中遇到的问题
  16. docker~aspnetcore2.0镜像缺少libgdiplus问题
  17. 201771010141 周强《面向对象设计 java》第十五周实验总结
  18. leetcode中的python学习
  19. flowable6.4.1+springboot使用dmn
  20. 一步步实现windows版ijkplayer系列文章之五——使用automake生成makefile

热门文章

  1. 最新解决VS2017+ Mysql + EF 创建实体数据模型 闪退的办法
  2. String、StringBuffer与StringBuilder之间区别 .RP
  3. MSSQL数据库设计心得
  4. Django之QuerySet 创建对象
  5. CF862B Mahmoud and Ehab and the bipartiteness 二分图染色判定
  6. luogu2155 [SDOI2008]沙拉公主的困惑
  7. python 矩阵(mat)操作
  8. 2019-5-1 maven学习笔记
  9. mybatis和jdbc分析
  10. springcloud系列三 搭建服务模块