代码实现特定 文件的复制粘贴。

如将D盘的大作业复制到F盘。
出现过拒绝访问的错误:你要读取文件,但路径是目录。

public class Demo1 {public static void main(String[] args) throws IOException {File srcPath = new File("D:\\大作业");File dstPath = new File("E:\\");copyDir(srcPath,dstPath);}private static void copyDir(File srcPath,File dstpath) throws IOException{if(srcPath.isDirectory()){//要复制的是一个文件夹File newFolder = new File(dstpath, srcPath.getName());//更新目录if(!newFolder.exists()) newFolder.mkdir();//创建目录File[] files = srcPath.listFiles();//得到目录下的文件for (File f:files//遍历文件) {copyDir(f,newFolder);}}else copy(srcPath,dstpath);}private static void copy(File srcfile, File dstPath) throws IOException {//字节缓冲流读写File dstFile = new File(dstPath,srcfile.getName());//目的文件BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));//字节缓冲输入流BufferedOutputStream bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));//字节缓冲输出流int len;byte[] b=new byte[1024];while((len=bufferedInputStream.read(b))!=-1){bufferedOutputStream.write(b,0,len);}bufferedInputStream.close();bufferedOutputStream.close();}
}

  1. try catch finally
    private static void copy(File srcfile, File dstPath)  {BufferedInputStream bufferedInputStream = null;//字节缓冲输入流File dstFile = new File(dstPath,srcfile.getName());//目的文件BufferedOutputStream bufferedOutputStream = null;//字节缓冲输出流try {bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));int len;byte[] b=new byte[1024];while((len=bufferedInputStream.read(b))!=-1){bufferedOutputStream.write(b,0,len);}} catch (IOException e) {e.printStackTrace();}finally {if(bufferedInputStream!=null){try {bufferedInputStream.close();} catch (IOException e) {e.printStackTrace();}}if(bufferedOutputStream!=null){try {bufferedOutputStream.close();} catch (IOException e) {e.printStackTrace();}}}}
  1. JDK 7以后可以
try(定义流对象){...
}catch(...){...
}自动释放资源,不用close
    private static void copy(File srcfile, File dstPath)  {File dstFile = new File(dstPath,srcfile.getName());//目的文件try(BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));BufferedOutputStream bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));){int len;byte[] b=new byte[1024];while((len=bufferedInputStream.read(b))!=-1){bufferedOutputStream.write(b,0,len);}}catch(IOException e){e.printStackTrace();}}
  1. JDK 9之后可以
定义流对象
定义流对象
try(流对象){}catch(...){....
}
自动释放资源
private static void copy(File srcfile, File dstPath) throws IOException {File dstFile = new File(dstPath,srcfile.getName());//目的文件BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(srcfile));//字节缓冲输入流BufferedOutputStream bufferedOutputStream = new BufferedOutputStream((new FileOutputStream(dstFile)));//字节缓冲输出流try(bufferedInputStream;bufferedOutputStream){int len;byte[] b=new byte[1024];while((len=bufferedInputStream.read(b))!=-1){bufferedOutputStream.write(b,0,len);}}catch (IOException e){e.printStackTrace();}}

复制文件以及异常处理相关推荐

  1. IO流案例,集合到文件数据排序、复制单级和多级文件夹及复制文件的异常处理

    目录 一.集合到文件数据排序 二.复制单级文件夹 三.复制多级文件夹 四.复制文件的异常处理 基本做法: JDK7版本改进: JDK9版本改进: 一.集合到文件数据排序 需求: 键盘录入5个学生信息( ...

  2. 复制文件的异常处理【应用】

    基本做法 public class CopyFileDemo {public static void main(String[] args) {}//try...catch...finallypriv ...

  3. [Java基础]复制文件的异常处理try...catch...finally的做法

    代码如下: package ErrorOperatorPack;import java.io.FileReader; import java.io.FileWriter; import java.io ...

  4. 字节流的 创建 写入文字 复制文件

    // 字节流文件的创建   复制 import java.io.*; import javax.imageio.stream.FileImageInputStream; public class Fi ...

  5. IO流之字节流 +字符流 (复制文件)

    复制的本质,如字节流字节数组复制,根据方法.read()的一次一次2个字节的读取,第一次读出的是ab 长度 2,第二次读出cd长度2,第三次读出e 长度1,但是还没结束,第四次读出0,长度-1,结束循 ...

  6. copy 修改时间_DAY5-step3 Python用shutil.copy(), shutil.copystat()复制文件

    Python复制文件的方法 Python提供了内置函数,可使用操作系统Shell工具轻松复制文件. 以下命令用于复制文件 shutil.copy(src,dst) 以下命令用于复制带有元数据信息的文件 ...

  7. scp服务器复制命令跳过已有的文件夹,Linux scp命令复制文件到其它服务器上

    例如:我想将59.64.30.101中的文件复制到59.64.28.78服务器.步骤如下: 1.59.64.30.101终端执行如下命令 #ssh-keygen -t rsa 2.密钥生成后会在/ro ...

  8. JAVA IO流复制文件夹及里面的所有文件

    public static void main(String[] args) throws Exception {//复制到哪个路径(path)中String path = "E:\\mai ...

  9. Linux10-归档、系统间复制文件

    目录 一.tar命令 二.scp.sftp命令 三.rsync命令 一.tar命令 tar命令可以归档文件.目录,提取创建的归档文件,同时进行压缩解压缩.使用tar选项时不需要加-,下面是常用的tar ...

最新文章

  1. 自学python要下载什么软件-Python学习需要安装的工具
  2. Google官方 详解 Android 性能优化【史诗巨著之内存篇】
  3. cxf 服务器响应超时时间,CXF日志响应时间
  4. 几款常用的ble调试app(nRF Connect、BLE调试助手、LightBlue)
  5. 三菱fx2n做从站的modbus通讯_第476期丨关于三菱PLC中除法的问题;请教一个监测电流的小物件或方法...
  6. 1-junos基本操作
  7. 高仿项目协作工具【Worktile】,从零带你一步步实现组织架构、网盘、消息、项目、审批等功能
  8. C#抖音无水印视频地址解析
  9. QQ空间相册批量下载qq相册批量下载专家比骨头小林还厉害
  10. 分布式计算、并行计算、网格计算
  11. 头歌java 实训 答案 代码 java入门
  12. 掘金企服:ICP经营许可证和ICP备案的区别 1
  13. Linux中fork函数详解
  14. Linux文件系统及文件储存方式
  15. [英语竞赛] 知识整理
  16. 前端js是什么意思?
  17. 解决各大浏览器下载文件,文件名中文乱码的问题
  18. 2018面试题目总结1
  19. 欧几里得算法和扩展欧几里得算法——杨子曰数学
  20. css background-image 属性

热门文章

  1. CentOS 7安装gitlab服务器
  2. Linux(Windows)下如何改变网卡的LinkSpeed工作模式
  3. vector int string 化
  4. 让textarea完全显示文章并且不滚动、不可拖拽、不可编辑
  5. Android后退事件的处理
  6. 验证视图状态 MAC 失败。如果此应用程序由网络场或群集承载,请确保 配置指定了相同的 validationKey 和验证算法。不能在群集中使用 AutoGenerate...
  7. 检查当前ORACLE连接数
  8. 挑选适合自己的公司——网络工程师你是否真的已经倒下(二)
  9. Windows数据类型探幽——千回百转你是谁?(2)
  10. Hi35xx GPIO在ko中设置