Copy the code code as follows:
package com.chen.lucene.image;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class Change2Image
{
/**Copy files
*
* @author chen_weixian
* Mar 11, 2012 11:33:19 PM
* @param path The path to the file to be copied
* @param savePath file save path (the path copied to)
* @throwsException
*/
public void change2Image(String path, String savePath) throws Exception
{
File file = new File(path);
if (!file.exists())
{
System.out.println("File does not exist!");
return ;
}
//Create the copied path if it does not exist
File saveFile = new File(savePath);
if (!saveFile.exists())
{
saveFile.mkdirs();
}
//Full path of new file
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";
// Convert files containing .tbi format into .jpg format
savePathNew = savePath + File.separator + (fbean.getName().replaceAll(".tbi", ".jpg"));
// Start copying
copy(fbean,new File(savePathNew));
}
}
}
/**Copy file
*
* @author chen_weixian
* Mar 11, 2012 11:31:59 PM
* @param fromFile
* @param toFile
* @throwsException
*/
private static void copy(File fromFile, File toFile) throws Exception{
if (!fromFile.exists())
{
System.out.println("The source file is empty!");
}
if (!toFile.exists())
{
System.out.println("Create new file...");
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();
}
/** test
* @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/March data package(1)/March data package";
String savePath = "E:/temp/img";
Change2Image change2Image = new Change2Image();
try
{
change2Image.change2Image(path, savePath);
}
catch (Exception e)
{
e.printStackTrace();
}
System.out.println("Complete");
}
}