复制单级文件夹,复制过去后,把原文夹里面的 .jpg文件,后缀名改成 .png
public class CopyFileDemo {
public static void main(String[] args) throws IOException {
//复制单级文件夹并该后缀名
//1.封装源文件夹
File srcFolder = new File("D:\\test");
//2.封装目标文件夹
File targetFolder = new File("E:\\test");
if (!targetFolder.exists()) {
targetFolder.mkdirs();
}
//复制文件夹
copyFolder(srcFolder, targetFolder);
System.out.println("复制完成!");
}
private static void copyFolder(File srcFolder, File targetFolder) throws IOException {
//获取源文件夹下所有的文件,复制并改名
File[] files = srcFolder.listFiles();
for (File f : files) {
copyFiles(f, targetFolder);
}
}
private static void copyFiles(File f, File targetFolder) throws IOException {
// System.out.println(targetFolder + "\\" + f.getName());
//读取源文件
FileInputStream in = new FileInputStream(f);
FileOutputStream out = null;
if (f.getName().endsWith(".jpg")) {
String newName = f.getName().substring(0, f.getName().lastIndexOf("."));
out = new FileOutputStream(targetFolder + "\\" + newName + ".png");
} else {
out = new FileOutputStream(targetFolder + "\\" + f.getName());
}
byte[] bytes = new byte[1024 * 8];
int len = 0;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
in.close();
out.close();
}
}
复制多级文件夹
public class CopyFile {
public static void main(String[] args) throws IOException {
File oldfile = new File("c:\\0417");
File newfile = new File("c:\\新建文件夹\\3");
if (newfile.exists()) {
System.out.println("文件名冲突");
return;
} else {
newfile.mkdirs();
}
CopyFiles(oldfile, newfile);
}
private static void CopyFiles(File oldfile, File newfile) throws IOException {
File[] files = oldfile.listFiles();
for (File file1 : files) {
if (file1.isDirectory()) {
File file = new File(newfile + "\\" + file1.getName());
file.mkdirs();
System.out.println(file.getName());
CopyFiles(file1, file);
} else {
copysinglefile(file1,newfile);
}
}
private static void copysinglefile(File oldfile, File newfile) throws IOException {
//FileOutputStream out = new FileOutputStream(oldfile);
// FileoutputStream in = new FileOutputStream(newfile+"\\"+oldfile.getName());
FileInputStream in = new FileInputStream(oldfile);
FileOutputStream out = new FileOutputStream(newfile + "\\" + oldfile.getName());
byte[] bytes = new byte[1024 * 8];
int len = 0;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
}
in.close();
out.close();
}
}
复制多级文件夹,复制过去后,把原文夹里面的 .jpg文件,后缀名改成 .png
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
public class copyrename2 {
public static void main(String[] args) throws IOException {
File oldfile = new File("c:\\0417");
File newfile = new File("c:\\新建文件夹\\8");
newfile.mkdirs();
CopyFiles(oldfile,newfile);
}
private static void CopyFiles(File oldfile, File newfile) throws IOException {
File[] files = oldfile.listFiles();
for (File file1 : files) {
if (file1.isDirectory()){
//如果是文件夹,就创建新的文件夹,并进行递归遍历文件夹内部的内容
File file = new File(newfile + "\\" + file1.getName());
file.mkdirs();
CopyFiles(file1,file);
} else if (file1.getName().endsWith(".jpg")){
//获取源文件名字
String s=file1.getName();
//更改后缀名
s=s.substring(0,s.lastIndexOf(".")).concat(".png");
FileOutputStream out = new FileOutputStream( new File(newfile+"\\"+s));
copysinglefile(file1,out);
}else {
FileOutputStream out = new FileOutputStream(newfile + "\\" + file1.getName());
copysinglefile(file1,out);
}
}
}
private static void copysinglefile(File oldfile, FileOutputStream out) throws IOException {
FileInputStream in = new FileInputStream(oldfile);
byte[] bytes = new byte[1024 * 8];
int len=0;
while ((len=in.read(bytes))!=-1){
out.write(bytes,0,len);
}
in.close();
out.close();
}
}