java代码实现文件的复制及更改后缀

来源:互联网 发布:人工智能就业 知乎 编辑:程序博客网 时间:2024/06/05 17:40

package com.chen.lucene.image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Change2Image
{
 
 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));     
   }
  }
 }
 
 
 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(); 
    } 

 
 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(“完成”);
 }
}

原创粉丝点击