java复制文件工具类
2012-09-17 18:11
375 查看
public int Copy(String source_name,String dest_name,int type) throws IOException { File source_fileFile=new File(source_name); File dest_fileFile=new File(dest_name); FileInputStream sourceFileInputStream=null; FileOutputStream destination=null; byte[] buffer; int bytes_read; int result=0; try { if (!source_fileFile.exists()||!source_fileFile.isFile()) { throw new RuntimeException("源文件不存在!"); } if (!source_fileFile.canRead()) { throw new RuntimeException("源文件不能读!"); } if (dest_fileFile.exists()) { if (dest_fileFile.isFile()) { if (type==1) {//覆盖目标文件 dest_fileFile.delete(); result=1; } else {//不覆盖目标文件! result=2; return result; } } else { throw new RuntimeException("目标是目录不是文件!"); } } else { File parentdir=new File(dest_fileFile.getParent()); if (!parentdir.exists()) { throw new RuntimeException("目标路径不存在!"); } if (!parentdir.canWrite()) { throw new RuntimeException("目标路径不可写!"); } //复制文件 sourceFileInputStream=new FileInputStream(source_fileFile); destination=new FileOutputStream(dest_fileFile); buffer=new byte[1024]; while(true) { bytes_read=sourceFileInputStream.read(buffer); if (bytes_read==-1) { break; } destination.write(buffer, 0, bytes_read); } } } finally { if (sourceFileInputStream!=null) { try { sourceFileInputStream.close(); } catch (IOException e) { } } if (destination!=null) { try { destination.close(); } catch (IOException e) { // TODO: handle exception } } return result; } }
相关文章推荐
- java文件整理(二)----对文件工具类(复制,删除,移动)
- java 文件复制 文件夹复制工具类
- java文件整理(二)----对文件工具类(复制,删除,移动)
- java文件操作工具类实现复制文件和文件合并
- java文件整理(二)----对文件工具类(复制,删除,移动)
- Java中实现复制文件到文件,复制文件到文件夹,复制文件夹到文件,删除文件,删除文件夹,移动文件,移动文件夹的工具类
- java操作文件工具类( 新建、复制、删除、移动文件(夹))
- Java复制文件工具类
- JAVA文件工具类之——文件复制、内容获取、目录获取、文件转换
- Java文件操作工具类fileUtil实例【文件增删改,复制等】
- Java IO中的文件复制实例(原创)
- java中利用IO流复制文件夹和文件
- JAVA工具类(9)--属性文件properties的读取
- Java中实现文件复制操作代码
- java 从一个文件中读取内容 复制到 另一个文件中
- Java实现文件或文件夹的复制到指定目录
- java: 复制文件最快方法
- 【Java IO】NIO复制文件
- JAVA 如何创建/删除/修改/复制目录及文件
- Java IO学习【8】利用缓冲区进行文件的复制