国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java 實(shí)現(xiàn)文件復(fù)制和格式更改的實(shí)例

2019-11-26 16:14:48
字體:
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:

package com.chen.lucene.image;

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

public class Change2Image
{
 /**復(fù)制文件
  *
  * @author chen_weixian
  * Mar 11, 2012   11:33:19 PM
  * @param path 需要復(fù)制文件的路徑
  * @param savePath 文件保存路徑(復(fù)制到的路徑)
  * @throws Exception
  */
 public void change2Image(String path, String savePath) throws Exception
 {
  File file = new File(path);
  if (!file.exists())
  {
   System.out.println("文件不存在!");
   return ;
  }
  // 復(fù)制到的路徑如不存在就創(chuàng)建
  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格式的轉(zhuǎn)化為.jpg格式 
    savePathNew = savePath + File.separator + (fbean.getName().replaceAll(".tbi", ".jpg"));
    // 開始復(fù)制
             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("創(chuàng)建新文件。。");
   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(); 

    } 


 /** 測(cè)試
  * @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月份數(shù)據(jù)包(1)/3月份數(shù)據(jù)包";
  String savePath = "E:/temp/img";
  Change2Image change2Image = new Change2Image();
  try
  {
   change2Image.change2Image(path, savePath);
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  System.out.println("完成");
 }

}

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 阜康市| 沙坪坝区| 鄂州市| 抚松县| 伊吾县| 土默特左旗| 灵丘县| 红桥区| 柳江县| 闸北区| 常德市| 宽甸| 宜春市| 平邑县| 兴文县| 桃园县| 敖汉旗| 通河县| 大余县| 视频| 铁岭县| 安国市| 丹巴县| 漳州市| 秦安县| 高州市| 金沙县| 福鼎市| 驻马店市| 阿巴嘎旗| 沁水县| 大连市| 肃宁县| 琼中| 增城市| 玛沁县| 建德市| 正安县| 新绛县| 原阳县| 江达县|