import java.util.*;
import java.io.*;
class CopyFilesWithName
{
public static void main(String[] args) throws Exception
{
File srcFile = new File("E:\\QQ邮箱");
File destFile = new File("F:\\lin");
if(!destFile.exists())
{
destFile.mkdir();
}
File[] files = srcFile.listFiles(new FilenameFilter(){
public boolean accept(File dir,String name)
{
return new File(dir,name).isFile()&&name.endsWith(".txt");
}
});
for(File file:files)
{
String name = file.getName();
File deFile = new File(destFile,name);
copy(file,deFile);
}
File[] endFiles = destFile.listFiles();
for(File file:endFiles)
{
String fileName = file.getName();
fileName = fileName.replace("txt","java");
File resultFile = new File(destFile,fileName);
file.renameTo(resultFile);
}
}
public static void copy(File file,File deFile) throws Exception
{
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(deFile));
BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
int len = 0;
byte[] bytes = new byte[1024];
while((len=bis.read(bytes))!=-1)
{
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
}
|
|