java 实现文件复制和格式更改的实例

时间:2023-10-21 08:07:49 


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

}

标签:文件复制,格式更改
0
投稿

猜你喜欢

  • JPA 加锁机制及@Version版本控制方式

    2022-10-06 10:57:58
  • Android实现中轴旋转特效 Android制作别样的图片浏览器

    2023-07-03 00:37:16
  • Android入门之AlertDialog用法实例分析

    2023-12-16 02:27:02
  • Java基于JDK 1.8的LinkedList源码详析

    2021-07-15 03:29:26
  • Java基于IO流读取文件的方法

    2023-08-11 16:23:57
  • Java编程泛型限定代码分享

    2023-11-09 17:46:32
  • ProtoBuf动态拆分Gradle Module解析

    2022-01-06 17:45:21
  • Elasticsearch查询及聚合类DSL语句宝典示例详解

    2023-01-10 18:51:46
  • 基于java Files类和Paths类的用法(详解)

    2021-08-11 11:22:55
  • 解决Android popupWindow设置背景透明度无效的问题

    2022-12-05 10:39:42
  • Java泛型中<?>和<T>的区别浅析

    2023-11-25 05:20:09
  • Android使用DocumentFile读写外置存储的问题

    2023-10-23 01:58:54
  • Java实现DFA算法对敏感词、广告词过滤功能示例

    2023-02-11 10:24:33
  • Android实现快递物流时间轴效果

    2021-12-03 23:56:54
  • Android ListView position详解及实例代码

    2023-10-29 03:33:15
  • Java枚举类用法实例

    2023-09-25 01:47:34
  • java两个integer数据判断相等用==还是equals

    2021-06-14 00:46:52
  • java设计模式--原型模式详解

    2023-11-25 05:08:24
  • Java数据结构 递归之迷宫回溯案例讲解

    2023-04-01 11:16:38
  • Bean实例化之前修改BeanDefinition示例详解

    2023-10-26 23:31:09
  • asp之家 软件编程 m.aspxhome.com