文件读取写入标准写法

 文件读取写入标准写法

public void copy(String src , String des){
  
  InputStream in = null;
  OutputStream out = null;
  
  try {
   in = new FileInputStream(src);//多态应用,FileInputStream继承InputStream
   out = new FileOutputStream(des);//多态应用,FileOutputStream继承OutputStream

   byte[] buf = new byte[1024];//申请1M内存,用于存放读入的数据,其实是作为缓冲(cache)

   int n ;
   while((n = in.read(buf))> 0){// read(buf))指将数据先读入buf内,当buf满时,跳出read方法,并返回buf的容量,然后赋值给n;当buf不满但已经读取完毕就返回buf的实际存放字节数
    out.write(buf, 0, n);
   }
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    in.close();//close()方法本身就有可能抛出异常,故而用try catch 包裹;如果抛出异常in就不能正常关闭但是资源还被占用,故而在finally里 in=null;
	       //无论抛出异常与否都将in赋值null,这样有利于垃圾回收机制将其回收;我其实挺建议这样写,涉及到不用资源时,先释放资源再赋值null这样使得垃圾回收更快
    out.close();
   } catch (IOException e) {
    e.printStackTrace();
   }finally{
    in = null;
    out = null;
   }
  }
 }

 源码解析:

public abstract class InputStream implements Closeable {
    private static final int MAX_SKIP_BUFFER_SIZE = 2048;

    public abstract int read() throws IOException;

    public int read(byte b[]) throws IOException {
        return read(b, 0, b.length);
    }

    public int read(byte b[], int off, int len) throws IOException {
        if (b == null) {
            throw new NullPointerException();
        } else if (off < 0 || len < 0 || len > b.length - off) {
            throw new IndexOutOfBoundsException();
        } else if (len == 0) {
            return 0;
        }
        int c = read();//读取一个字节,这也是为什么inputStream是字节流的原因
        if (c == -1) {
            return -1;
        }
        b[off] = (byte)c;
        int i = 1;
        try {
            for (; i < len ; i++) {
                c = read();
                if (c == -1) {
                    break;
                }
                b[off + i] = (byte)c;
            }
        } catch (IOException ee) {
        }
        return i;// read(buf))指将数据先读入buf内,当buf满时,跳出read方法,并返回buf的容量,然后赋值给n;当buf不满但已经读取完毕就返回buf的实际存放字节数
    }
}

 

 

public class FileInputStream extends InputStream//继承自InputStream
{
    public FileInputStream(String name) throws FileNotFoundException {
        this(name != null ? new File(name) : null);
    }
    public native int read() throws IOException;//调用本地方法和系统资源,读取一个字节,这也是为什么inputStream和其子类 是字节流的原因

    private native int readBytes(byte b[], int off, int len) throws IOException;//功能和inputStream的read(byte b[], int off, int len)方法一样,区别就是它是本地方法

    public int read(byte b[]) throws IOException {
        return readBytes(b, 0, b.length);
    }

    public int read(byte b[], int off, int len) throws IOException {
        return readBytes(b, off, len);
    }
}

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值