import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.swing.JOptionPane;
public class FileCopy {
public static void copyFile(File fis,File fos) throws IOException
{
String[] fisList = fis.list();
String fispath = fis.getAbsolutePath();
String fospath = fos.getAbsolutePath();
for(int i = 0; i < fisList.length; i ++)
{
File ftempin = new File(fispath, fisList[i]);
File ftempout = new File(fospath,fisList[i]);
if(ftempin.isFile())
{
FileInputStream infile = new FileInputStream(ftempin);
FileOutputStream outfile = new FileOutputStream(ftempout);
int c;
while((c = infile.read()) != -1)
outfile.write(c);
infile.close();
outfile.close();
ftempin.delete();//删除文件
}
else{
ftempout.mkdirs();//创建文件夹
copyFile(ftempin, ftempout);
}
}
fis.delete();//删除空目录
}
public static void main(String[] args) throws IOException {
String addressin = JOptionPane.showInputDialog("请输入复制的文件地址");
File filein = new File(addressin);
while(filein.exists() == false)
{
JOptionPane.showMessageDialog(null, "你输入的文件地址有误", "提示", JOptionPane.ERROR_MESSAGE);
addressin = JOptionPane.showInputDialog("请重新输入");
filein = new File(addressin);
}
String addressout = JOptionPane.showInputDialog("请输入要粘贴的地址");
File fileout = new File(addressout);
while(fileout.exists() == false)
{
JOptionPane.showMessageDialog(null, "你输入的文件地址有误", "提示", JOptionPane.ERROR_MESSAGE);
addressout = JOptionPane.showInputDialog("请重新输入");
fileout = new File(addressout);
}
File fileout2 = new File(fileout.getAbsoluteFile(),filein.getName());
fileout2.mkdirs();
if(filein.getCanonicalPath() == fileout2.getCanonicalPath())
{
JOptionPane.showMessageDialog(null, "无法移动" + filein.getName() + ": 源文件夹与目标文件夹相同", "错误", JOptionPane.ERROR_MESSAGE);
}
else{
copyFile(filein, fileout2);
JOptionPane.showMessageDialog(null, "复制成功", "提示", JOptionPane.INFORMATION_MESSAGE);
}
}
}
评论1
最新资源