0
点赞
收藏
分享

微信扫一扫

疯狂Java讲义习题11.3


题目描述:
实现一个工具类,该工具可实现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;    
= new FileOutputStream(dst+"\\"+src.substring(src.lastIndexOf('\\')+1));
= new FileInputStream(src);
int hasRead = 0;
byte[] bbuf = new byte[1024];
while ((hasRead = fis.read(bbuf)) > 0)
{
                 
.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())
{
             
(dst,src);
}
else
{
File dstFile = new File(dst+"//"+newFile.getName());
if (!dstFile.exists())
{
.mkdirs();
}
for (File file:fileList)
{
if (file.isFile())
(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





举报

相关推荐

0 条评论