黑马程序员 JAVA 五一论坛练习 —— 3 (5技术分)

------- android培训java培训、期待与您交流! --------- 


 
将C盘指定的一个文件夹(包含文件夹内的所有文件夹和所有文件,多层嵌套)复制到D盘中。贴出你的实现代码,并隐藏,回答正确即可获取技术分,根据代码质量酌情给分,最多可获得5分。
 
思路:用JFileChooser文件夹选择器来指定源文件或文件夹 
定义两个函数 copyfile用来打文件 (输出同名文件和子目录文件)
copydirectory 用来打文件夹(在D盘创建同名文件夹和子目录)

 
package  ;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.swing.JFileChooser;
public  class LX3 {
     static  String dird  =  "D:"; // D
     public  static  void main( String[] arg) {
        JFileChooser jfc  =  new JFileChooser();
        jfc.setDialogTitle( "");
        jfc.setDialogType(JFileChooser.OPEN_DIALOG); //  JFileChooser  "Open"
                                                     // 
        jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES); // 
         int res  = jfc.showOpenDialog( null); //  "Open File" 
         if (res  == jfc.APPROVE_OPTION) {
            File dir  = jfc.getSelectedFile();
             if (dir.isDirectory()) {
                copydirectory(dir, dir.getAbsolutePath(), dir.getName());
            }  else
                copyfile(dir.getAbsolutePath(), dir.getName());
        }
    }
     public  static  void copyfile( String path,  String name) // 
    { // 
        BufferedInputStream br  =  null;
        BufferedOutputStream bw  =  null;
         try {
            br  =  new BufferedInputStream( new FileInputStream(path));
        }  catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
            e.printStackTrace();
        }
         try {
            bw  =  new BufferedOutputStream( new FileOutputStream(dird
                     + File.separator  + name)); // separator便
        }  catch (FileNotFoundException e) {
             // TODO Auto-generated catch block
            e.printStackTrace();
        }
         byte[] b  =  new  byte[ 1024]; // 
         int x  =  0;
         try {
             while ((x  = br.read(b))  !=  -1) {
                 //System.out.println(x);
                bw.write(b);
                
            }
            bw.flush();
        }  catch (IOException e) {
             // TODO Auto-generated catch block
            e.printStackTrace();
        }
         try {
            br.close();
        }  catch (IOException e) {
             // TODO Auto-generated catch block
            e.printStackTrace();
        }
         try {
            bw.close();
        }  catch (IOException e) {
             // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
     public  static  void copydirectory(File dir,  String path,  String name) { // 
        File[] f  = dir.listFiles();
         boolean c  =  new File(dird  + File.separator  + name).mkdir(); // D
         for ( int x  =  0; x  < f.length; x ++) {
             if (f[x].isDirectory()) { //  
                name  += File.separator  + f[x].getName(); // 
                copydirectory(f[x], f[x].getAbsolutePath(), name); // 
            }  else {
                copyfile(f[x].getAbsolutePath(),
                        name  + File.separator  + f[x].getName()); // 
            }
        }
    }
}
 
到做第4题的时候才发现建立缓冲时大小用读取流中的available()方法 建立个正好的缓冲 
活动地址:
http://bbs.itheima.com/forum.php?mod=viewthread&tid=116270



------- android培训java培训、期待与您交流! ---------

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值