用FileChannel拷贝文件

简介:

 用filechannel更方便一些

 
  1. /**  
  2. Copy files, using two techniques, FileChannels and streams. 
  3. Using FileChannels is usually faster than using streams. 
  4. */ 
  5. public final class CopyFiles { 
  6.   
  7.  /* Change these settings before running this class. */ 
  8.   
  9.  /** The file to be copied. */ 
  10.  public static final String INPUT_FILE = "C:\\TEMP\\cottage.jpg"
  11.   
  12.  /** 
  13.   The name of the copy to be created by this class.   
  14.   If this file doesn't exist, it will be created, along with any  
  15.   needed parent directories.   
  16.  */ 
  17.  public static final String COPY_FILE_TO = "C:\\TEMP10\\cottage_2.jpg"
  18.   
  19.  /** Run the example. */ 
  20.  public static void main(String... aArgs) throws IOException{ 
  21.    File source = new File(INPUT_FILE); 
  22.    File target = new File(COPY_FILE_TO); 
  23.    CopyFiles test = new CopyFiles(); 
  24.    test.copyWithChannels(source, target, false); 
  25.    //test.copyWithStreams(source, target, false); 
  26.    log("Done."); 
  27.  } 
  28.  
  29.  /** This may fail for VERY large files. */ 
  30.  private void copyWithChannels(File aSourceFile, File aTargetFile, boolean aAppend) { 
  31.    log("Copying files with channels."); 
  32.    ensureTargetDirectoryExists(aTargetFile.getParentFile()); 
  33.    FileChannel inChannel = null
  34.    FileChannel outChannel = null
  35.    FileInputStream inStream = null
  36.    FileOutputStream outStream = null
  37.    try
  38.      try { 
  39.        inStream = new FileInputStream(aSourceFile); 
  40.        inChannel = inStream.getChannel(); 
  41.        outStream = new  FileOutputStream(aTargetFile, aAppend);         
  42.        outChannel = outStream.getChannel(); 
  43.        long bytesTransferred = 0
  44.        //defensive loop - there's usually only a single iteration : 
  45.        while(bytesTransferred < inChannel.size()){ 
  46.          bytesTransferred += inChannel.transferTo(0, inChannel.size(), outChannel); 
  47.        } 
  48.      } 
  49.      finally { 
  50.        //being defensive about closing all channels and streams  
  51.        if (inChannel != null) inChannel.close(); 
  52.        if (outChannel != null) outChannel.close(); 
  53.        if (inStream != null) inStream.close(); 
  54.        if (outStream != null) outStream.close(); 
  55.      } 
  56.    } 
  57.    catch (FileNotFoundException ex){ 
  58.      log("File not found: " + ex); 
  59.    } 
  60.    catch (IOException ex){ 
  61.      log(ex); 
  62.    } 
  63.  } 
  64.   
  65.  private void copyWithStreams(File aSourceFile, File aTargetFile, boolean aAppend) { 
  66.    log("Copying files with streams."); 
  67.    ensureTargetDirectoryExists(aTargetFile.getParentFile()); 
  68.    InputStream inStream = null
  69.    OutputStream outStream = null
  70.    try
  71.      try { 
  72.        byte[] bucket = new byte[32*1024]; 
  73.        inStream = new BufferedInputStream(new FileInputStream(aSourceFile)); 
  74.        outStream = new BufferedOutputStream(new FileOutputStream(aTargetFile, aAppend)); 
  75.        int bytesRead = 0
  76.        while(bytesRead != -1){ 
  77.          bytesRead = inStream.read(bucket); //-1, 0, or more 
  78.          if(bytesRead > 0){ 
  79.            outStream.write(bucket, 0, bytesRead); 
  80.          } 
  81.        } 
  82.      } 
  83.      finally { 
  84.        if (inStream != null) inStream.close(); 
  85.        if (outStream != null) outStream.close(); 
  86.      } 
  87.    } 
  88.    catch (FileNotFoundException ex){ 
  89.      log("File not found: " + ex); 
  90.    } 
  91.    catch (IOException ex){ 
  92.      log(ex); 
  93.    } 
  94.  } 
  95.   
  96.  private void ensureTargetDirectoryExists(File aTargetDir){ 
  97.    if(!aTargetDir.exists()){ 
  98.      aTargetDir.mkdirs(); 
  99.    } 
  100.  } 
  101.   
  102.  private static void log(Object aThing){ 
  103.    System.out.println(String.valueOf(aThing)); 
  104.  } 
  105. }  

 


本文转自sucre03 51CTO博客,原文链接:http://blog.51cto.com/sucre/652052,如需转载请自行联系原作者

相关文章
|
20小时前
|
云安全 人工智能 自然语言处理
|
5天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
310 116
|
8天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
550 51
Meta SAM3开源:让图像分割,听懂你的话
|
20天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
4天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
|
3天前
|
弹性计算 人工智能 Cloud Native
阿里云无门槛和有门槛优惠券解析:学生券,满减券,补贴券等优惠券领取与使用介绍
为了回馈用户与助力更多用户节省上云成本,阿里云会经常推出各种优惠券相关的活动,包括无门槛优惠券和有门槛优惠券。本文将详细介绍阿里云无门槛优惠券的领取与使用方式,同时也会概述几种常见的有门槛优惠券,帮助用户更好地利用这些优惠,降低云服务的成本。
263 132
|
8天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
386 29
|
14天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
704 224