`

Java 文件拷贝

    博客分类:
  • Java
阅读更多

 public static void saveFiles(String origPath, String destPath) {
  File file = new File(origPath);
  System.out.println("开始复制文件");
 
  InputStream in = null;
  BufferedOutputStream fos=null;
  try {
   fos= new BufferedOutputStream(new FileOutputStream(destPath));
   if (!file.exists() || file.isDirectory())
    throw new FileNotFoundException();
   in = new BufferedInputStream(new FileInputStream(file));
   byte[] temp = new byte[1024];
   int size = 0;
   while ((size = in.read(temp)) != -1) {
    fos.write(temp, 0, size);
   } 
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (UnsupportedEncodingException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }finally{
   try {
    fos.flush();
    fos.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
   try {
    in.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
  System.out.println("复制文件完成");
 }

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics