我也要面试了,要疯狂做题啊
- import java.io.*;
- class CopyDir
- {
- public static void main(String[] args) throws Exception
- {
- File filescr = new File("F:\\03java\\1016");
- File filedes = new File("E:\\");
- copy(filescr,filedes);
- }
- public static void copy(File filescr,File filedes) throws Exception
- {
-
- String dirName = filescr.getName();
- filedes = new File(filedes.getAbsolutePath()+"\\"+dirName);
- filedes.mkdir();
- File[] files = filescr.listFiles();
- for(File file:files)
- {
- if(file.isDirectory())
- {
- copy(file,filedes);
- }
- else
- {
- BufferedInputStream bis =
- new BufferedInputStream(new FileInputStream(file));
- String fileName = file.getName();
- File filedes1 = new File(filedes.getAbsolutePath()+"\\"+fileName);
- BufferedOutputStream bos =
- new BufferedOutputStream(new FileOutputStream(filedes1));
- int len=0;
- while((len=bis.read())!=-1)
- {
- bos.write(len);
- bos.flush();
- }
- bis.close();
- bos.close();
- }
- }
- }
- }
复制代码
|