题目描述:
实现一个工具类,该工具可实现copy功能,若被copy的对象是文件,程序将制定文件复制到制定目录下,如过被copy对象是目录,程序应将该目录及其目录下的所有文件复制到指令目录下。
代码如下:
-
import java.io.*;
-
public class cpft
-
{
-
public static void copyFile(String dst,String src) throws IOException
-
{
-
FileOutputStream fos = null;
-
FileInputStream fis = null;
-
fos = new FileOutputStream(dst+"\\"+src.substring(src.lastIndexOf('\\')+1));
-
fis = new FileInputStream(src);
-
int hasRead = 0;
-
byte[] bbuf = new byte[1024];
-
while ((hasRead = fis.read(bbuf)) > 0)
-
{
-
-
fos.write(bbuf,0,hasRead);
-
}
-
-
}
-
public static void copy(String dst,String src) throws IOException
-
{
-
File newFile = new File(src);
-
File[] fileList = newFile.listFiles();
-
//File dstFile = new File(dst+"//"+newFile.getName());
-
if (newFile.isFile())
-
{
-
-
copyFile(dst,src);
-
}
-
else
-
{
-
File dstFile = new File(dst+"//"+newFile.getName());
-
if (!dstFile.exists())
-
{
-
dstFile.mkdirs();
-
}
-
for (File file:fileList)
-
{
-
if (file.isFile())
-
copyFile(dstFile.getAbsolutePath(),file.getAbsolutePath());
-
else
-
copy(dstFile.getAbsolutePath(),file.getAbsolutePath());
-
}
-
}
-
}
-
public static void main(String[] args) throws IOException
-
{
-
-
String src = "F:\\paper";
-
String dst = "F:\\冰点文库";
-
copy(dst,src);
-
System.out.println("finished Copy");
-
}
-
}
运行结果:(完成复制)
finished Copy
阅读(921) | 评论(0) | 转发(0) |