- public class CopyFile {
- public static void main(String [] args) throws Exception{
- File src=new File("F:\\test2");
- System.out.println(src.getAbsolutePath());
- File des=new File("F:\\copy_test2");
- File[] arr=src.listFiles();
- for(File a:arr){
- if(a.isDirectory())
- copyDirectory(a,src,des);
- else
- copyFile(a,src,des);
- }
-
- }
- public static void copyFile(File a,File src,File des) throws Exception{
- //BufferedInputStream buis=new BufferedInputStream(new FileInputStream(src.getAbsolutePath()));
- FileInputStream fis=new FileInputStream(a.getAbsolutePath());
- //BufferedOutputStream buos=new BufferedOutputStream(new FileOutputStream(des.getAbsolutePath()));
- FileOutputStream fos=new FileOutputStream(a.getAbsolutePath());
- int len=0;
- while((len=fis.read())!=-1){
- fos.write(len);
- }
- }
- public static void copyDirectory(File f,File src,File des) throws Exception{
- File name=new File(f.getAbsolutePath(),f.getName());
- name.mkdir();
- File[] arr=src.listFiles();
- for(File a:arr){
- if(a.isFile()){
- copyFile(a,src,des);
- }
- else{
- copyDirectory(a,src,des);
- }
- }
-
- }
- }
复制代码 错误是:
Exception in thread "main" java.lang.StackOverflowError
at java.lang.ref.Finalizer.<init>(Finalizer.java:85)
at java.lang.ref.Finalizer.register(Finalizer.java:90)
目前功能也没用实现,请大神指导下~
|