字节流的读和写

来源:互联网 发布:sketch mac 汉化补丁 编辑:程序博客网 时间:2024/06/07 02:17
public void copy(){
 InputStream in=null;
 OutputStream out=null;
 try {
  in=new FileInputStream("D:/xx.txt");
  out=new FileOutputStream("F:/xx.txt");
  int len=0;
  byte[] by=new byte[1024];
  while((len=in.read(by))!=-1){
   //写入byte数组中的数据,读取几个字节就写入几个字节
   out.write(by,0,len);
  }
  
 } catch (Exception e) {
  e.printStackTrace();
 }finally{
  try {
   //关闭流,先建立后关闭
   out.close();
   in.close();
  } catch (Exception e) {
   e.printStackTrace();
  }
  
 }
}
 
0 0
原创粉丝点击