java 实现文件复制,并且更改格式

复制代码
package com.chen.lucene.image;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class Change2Image
{
 /**复制文件
  * 
  * @author chen_weixian
  * Mar 11, 2012   11:33:19 PM
  * @param path 需要复制文件的路径
  * @param savePath 文件保存路径(复制到的路径)
  * @throws Exception
  */
 public void change2Image(String path, String savePath) throws Exception
 {
  File file = new File(path);
  if (!file.exists())
  {
   System.out.println("文件不存在!");
   return ;
  }
  // 复制到的路径如不存在就创建
  File saveFile = new File(savePath);
  if (!saveFile.exists())
  {
   saveFile.mkdirs();
  }
  // 新文件全路径
  String savePathNew = "";
  for (File fbean : file.listFiles())
  {
   if (fbean.isFile())
   {
    System.out.println(fbean.getName() + "\t" + fbean.getAbsolutePath());
//    savePathNew = savePath + File.separator + fbean.getName()+ ".jpg";
    // 把文件名称中含有.tbi格式的转化为.jpg格式  
    savePathNew = savePath + File.separator + (fbean.getName().replaceAll(".tbi", ".jpg"));
    // 开始复制 
             copy(fbean ,new File(savePathNew));      
   }
  }
 }
 
 /**拷贝文件
  * 
  * @author chen_weixian
  * Mar 11, 2012   11:31:59 PM
  * @param fromFile
  * @param toFile
  * @throws Exception
  */
 private static void copy(File fromFile, File toFile) throws Exception{  
  if (!fromFile.exists())
  {
   System.out.println("来源文件为空!");
  }
  if (!toFile.exists())
  {
   System.out.println("创建新文件。。");
   toFile.createNewFile();
  }
  FileInputStream  fis = new FileInputStream(fromFile);
        System.out.println("fromFile :" + fromFile.getAbsolutePath());
        FileOutputStream fos = new FileOutputStream(toFile);
        System.out.println("toFile :" + toFile.getAbsolutePath());
  
        int len = 0;  
        byte[] buf = new byte[1024];  
        while((len = fis.read(buf)) != -1){  
         fos.write(buf,0,len);  
        }
        
        fis.close();  
        fos.close();  

    }  


 /** 测试
  * @author chen_weixian
  * Mar 11, 2012   10:19:56 PM
  * @param args
  */
 public static void main(String[] args)
 {
//  String path = "E:/temp";
  String path = "E:/temp/3月份数据包(1)/3月份数据包";
  String savePath = "E:/temp/img";
  Change2Image change2Image = new Change2Image();
  try
  {
   change2Image.change2Image(path, savePath);
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  System.out.println("完成");
 }

}
复制代码

 

posted on   陈惟鲜的博客  阅读(672)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

< 2013年3月 >
24 25 26 27 28 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6
点击右上角即可分享
微信分享提示