java 實現(xiàn)文件復(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();
}
/** 測試
* @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("完成");
}
}
相關(guān)文章
springboot集成kafka消費(fèi)手動啟動停止操作
這篇文章主要介紹了springboot集成kafka消費(fèi)手動啟動停止操作,本文給大家介紹項目場景及解決分析,結(jié)合實例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-09-09Java?中?Class?Path?和?Package的使用詳解
這篇文章主要介紹了Java?中?Class?Path和Package的使用詳解,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-08-08解析web.xml中在Servlet中獲取context-param和init-param內(nèi)的參數(shù)
本篇文章是對web.xml中在Servlet中獲取context-param和init-param內(nèi)的參數(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-07-07eclipse輸出Hello World的實現(xiàn)方法
這篇文章主要介紹了eclipse輸出Hello World的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11解決springboot bean中大寫的字段返回變成小寫的問題
這篇文章主要介紹了解決springboot bean中大寫的字段返回變成小寫的問題,具有很好的參考價值希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Java數(shù)據(jù)結(jié)構(gòu)之線段樹中的懶操作詳解
對于線段樹,若要求對區(qū)間中的所有點都進(jìn)行更新,可以引入懶操作。懶操作包括區(qū)間更新和區(qū)間查詢操作。本文將通過一個示例和大家詳細(xì)聊聊線段樹中的懶操作,需要的可以參考一下2022-10-10