java 简单的拷贝文件夹

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
 
class Wenjian implements Runnable{
    static String str="C:\\Users\\cdlx2016\\Desktop\\实验";
    static String str10="C:\\Users\\cdlx2016\\Desktop\\实验2";
 
    static int a=0;
     //static File file = new File(str);
      @Override
    public void run() {
          synchronized ("222") {
               
              paly(str10);
          }
    }
      public void paly(String str11){
     
          File file1 = new File(str11);
          File file = new File(str);
          //创建新的文件夹
          file1.mkdirs();
            File[] files = file.listFiles();
            for (int i = 0;i<files.length;i++) {
                //System.out.println(files[i].getName());
                if(files[i].isFile()){
                    //拼接新的文件地址
                    String s = str11+"\\"+files[i].getName();
                    //原来的文件地址
                    String s1 = files[i].getPath();
                        ShuRu(s1,s);
                }
                if (files[i].isDirectory()) {
                     
                    str=files[i].getPath();
                    //拼接新的文件夹地址
                    str10=str11+"\\"+files[i].getName();
                     
                    paly(str10);
            }
       }
           
      }
      public static void ShuRu(String str4,String s) {
            File file =new File(str4);
                 
                FileInputStream iputFileInputStream=null;
                try {
                    iputFileInputStream = new FileInputStream(file);
                    byte[] b = new byte[1024];
                    int cuont=0;
                    try {
                        while ((cuont=iputFileInputStream.read(b))!=-1) {
                            System.out.println(new String(b, 0, cuont));
                            ShuChu(b,s);   
                        }
                    } catch (IOException e) {
                         System.out.println("你的硬盘有问题");
                         throw new RuntimeException(e);
                    }
                } catch (FileNotFoundException e) {
                     System.out.println("你写入的文件有误");
                       throw new RuntimeException(e);
                }finally{
                     
                    try {
                        iputFileInputStream.close();
                    } catch (IOException e) {
                         System.out.println("无法关闭");
                         throw new RuntimeException(e);
                    }
                     
                }
                 
            }
      public static void ShuChu(byte[] bb ,String s){
        //C:\Users\cdlx2016\Desktop
          a++;
        File file =new File(s);
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(file,true);
            byte [] b = bb;
            try {
                outputStream.write(b);
            } catch (IOException e) {
                 System.out.println("你的硬盘有问题");
                 throw new RuntimeException(e);
            }
        } catch (FileNotFoundException e) {
               System.out.println("你写出的文件有误");
               throw new RuntimeException(e);
        }finally{
            try {
                outputStream.close();
            } catch (IOException e) {
                 System.out.println("无法关闭");
                 throw new RuntimeException(e);
                  
            }
        }
         
         
    }
}
 
 
 
public class Work1 {
 
    public static void main(String[] args) {
        // TODO Auto-generated method stub
     
//(七)输入两个文件夹名称,将A文件夹内容全部拷贝到B文件夹,要求使用多线程来操作。 必做
        Wenjian ww=new Wenjian();
          Thread t = new Thread(ww,"下载一");
          Thread t1 = new Thread(ww,"下载一");
          t.start();
          t1.start();
          
           
         
    }
 
}

 

 

posted @   zerobased  阅读(173)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示