A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 王震阳老师   /  2014-4-30 10:34  /  54061 人查看  /  602 人回复  /   4 人收藏 转载请遵从CC协议 禁止商业使用本文

回复看下!!!!!!!!!!!!
回复 使用道具 举报
来看看
回复 使用道具 举报
自己努力的

DirectoryCopy.rar

1.07 KB, 阅读权限: 100, 下载次数: 2

评分

参与人数 1技术分 +2 收起 理由
王震阳老师 + 2 赞一个!

查看全部评分

回复 使用道具 举报
我要技术分
回复 使用道具 举报
学习基础中。。
回复 使用道具 举报
瞅瞅。。。。。
回复 使用道具 举报
看看                                      
回复 使用道具 举报
提交这一题!

Test5.rar

1.04 KB, 阅读权限: 100, 下载次数: 1

提交!

评分

参与人数 1技术分 +3 收起 理由
王震阳老师 + 3 赞一个!

查看全部评分

回复 使用道具 举报
为啥没分呢 我的答案在64楼 谢谢了
回复 使用道具 举报
public class CopyFile
{
public static void main(String[] args) throws Exception
{
  String source = "c:\\test";
  String dest = "d:\\trytest";
  write2Dir(source,dest);
}


public static void write2file(File SourceFile,File DestFile)
{
  BufferedInputStream inputStream = null;
  BufferedOutputStream outputStream = null;
  try
  {
   inputStream = new BufferedInputStream(new FileInputStream(SourceFile));
   outputStream = new BufferedOutputStream(new FileOutputStream(DestFile));
   
   byte[] buf = new byte[1024];
   int len = 0;
   while((len = inputStream.read(buf)) != -1)
   {
    outputStream.write(buf,0,len);
   }
   outputStream.flush();
  }
  catch (Exception e)
  {
   e.printStackTrace();
  }
  finally
  {
   try
   {
    if(inputStream != null)
     inputStream.close();
    if(outputStream!=null)
     outputStream.close();
   }
   catch (Exception e2)
   {
    e2.printStackTrace();
   }
  }
}

public static void write2Dir(String sourceDir,String desDir) throws IOException
{
  (new File(desDir)).mkdirs();
  
  File[] files = (new File(sourceDir)).listFiles();
  for(int i=0; i<files.length; i++)
  {
   if(files.isFile())
   {
    File SourceFile = files;
    File DestFile = new File(new File(desDir).getCanonicalPath()+(String)File.separator+files.getName());
    write2file(SourceFile,DestFile);
   }
   if(files.isDirectory())
   {
    String source = sourceDir + File.separator  +  files.getName();
    String dest = desDir + File.separator  +  files.getName();
    write2Dir(source, dest);
   }
  }
}
}


write2dir.rar

763 Bytes, 下载次数: 123

write2dir.rar

763 Bytes, 下载次数: 131

评分

参与人数 1技术分 +3 收起 理由
王震阳老师 + 3 赞一个!

查看全部评分

回复 使用道具 举报
先回复
回复 使用道具 举报

汗啊,找了半天,也没有找到让代码隐藏- -#。
麻烦老师下载下来看看吧。。。

TestCopy.rar

947 Bytes, 阅读权限: 150, 下载次数: 3

评分

参与人数 1技术分 +5 收起 理由
王震阳老师 + 5 赞一个!

查看全部评分

回复 使用道具 举报
版主,我提交了答案,怎么不给我技术分?
回复 使用道具 举报
版主先生,请抽空查阅答案,酌情给点技术分!!!

CopyDirTest.zip

1.12 KB, 阅读权限: 100, 下载次数: 1

评分

参与人数 1技术分 +4 收起 理由
王震阳老师 + 4 赞一个!

查看全部评分

回复 使用道具 举报
楼主看题目我技术功力不够,文件夹多层嵌套,深度不确定,一时做不了啦,向你们学习啦。。。
回复 使用道具 举报
我来领取技术分啦

从C盘复制文件到D盘.rar

1.15 KB, 阅读权限: 100, 下载次数: 1

评分

参与人数 1技术分 +3 收起 理由
王震阳老师 + 3 赞一个!

查看全部评分

回复 使用道具 举报
看一下题目
回复 使用道具 举报
凑个热闹..................
回复 使用道具 举报
领题.......
回复 使用道具 举报
小马初长成 发表于 2014-5-1 15:34
凑个热闹..................

楼主酌情给点分吧
:loveliness:
  1. import java.io.*;

  2. /**

  3. * 复制文件夹或文件夹

  4. */

  5. public class CopyDirectory {

  6. // 源文件夹

  7. static String url1 = "c:/photos";

  8. // 目标文件夹

  9. static String url2 = "d:/tempPhotos";

  10. public static void main(String args[]) throws IOException {

  11. // 创建目标文件夹

  12. (new File(url2)).mkdirs();

  13. // 获取源文件夹当前下的文件或目录

  14. File[] file = (new File(url1)).listFiles();

  15. for (int i = 0; i < file.length; i++) {

  16. if (file[i].isFile()) {

  17. // 复制文件

  18. copyFile(file[i],new File(url2+file[i].getName()));

  19. }

  20. if (file[i].isDirectory()) {

  21. // 复制目录

  22. String sourceDir=url1+File.separator+file[i].getName();

  23. String targetDir=url2+File.separator+file[i].getName();

  24. copyDirectiory(sourceDir, targetDir);

  25. }

  26. }

  27. }

  28. // 复制文件

  29. public static void copyFile(File sourceFile,File targetFile)

  30. throws IOException{

  31. // 新建文件输入流并对它进行缓冲

  32. FileInputStream input = new FileInputStream(sourceFile);

  33. BufferedInputStream inBuff=new BufferedInputStream(input);
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马