如何将文件夹内容复制到java中的另一个文件夹?

时间:2016-11-15 16:13:49

标签: java copy jfilechooser subdirectory

我需要一个程序,用户选择2个目录,然后同步,A内容为B,B内容为A,全同步(A到b,b到A),

但我有问题, 1.我无法显示子文件夹内容,以及 我无法复制文件,我打了很多东西。 :(

使用实际结构,控制台显示我的工作空间文件夹的路径(我选择" C:/ Test-folder / Origin"和" C:/ Test-folder / dest& #34;

package JFileChooser;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.CopyOption;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

import javax.swing.JFileChooser;

public class Main {
    public static void main(String[] args) throws IOException {
          JFileChooser jf = new JFileChooser("C://Test-folder/Origin");
          jf.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
          JFileChooser jf2 = new JFileChooser("C://Test-folder/dest");
          jf2.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
          int status = jf.showOpenDialog(null);
          int status2 = jf2.showOpenDialog(null);

          if (status == JFileChooser.APPROVE_OPTION) {
//            File selectedFile = jf.getSelectedFile();
            } else if (status == JFileChooser.CANCEL_OPTION) {
              System.out.println("canceled");
            }

        if (status2 == JFileChooser.APPROVE_OPTION) {
//          File selectedFile2 = jf2.getSelectedFile();
          } else if (status == JFileChooser.CANCEL_OPTION) {
            System.out.println("canceled");
          }



        String[] strOrigen = jf.getSelectedFile().list();
        String[] strDestino = jf2.getSelectedFile().list();

//      File[] strOrigen = jf.getSelectedFile().listFiles();
//      File[] strDestino = jf2.getSelectedFile().listFiles();
//      
        System.out.println(strOrigen);
        for (String lista1 : strOrigen) for (String lista2 : strDestino){
            String filePath = lista1;
            String filePath2 = lista2;
            File file = new File(filePath);
            File file2 = new File(filePath2);

            System.out.println(strOrigen);


            Path path1 = Paths.get(file.getAbsolutePath());
            Path path2 = Paths.get(file2.getAbsolutePath());
//          String path3 = file.getAbsolutePath();
//          String path4 = file2.getAbsolutePath();
//          Path path5 = Paths.get(file.getAbsolutePath());
//          Path path6 = Paths.get(file2.getAbsolutePath());


            System.out.println(path1);
            System.out.println(path2);
            CopyOption[] options = new CopyOption[]{
                      StandardCopyOption.REPLACE_EXISTING,
                      StandardCopyOption.COPY_ATTRIBUTES
                    };
            Files.copy(path1, path2, options);


//            String in = jf.getSelectedFile().getAbsolutePath();
//            String out = jf.getSelectedFile().getPath();
//            File dest = new File(jf2.getSelectedFile().getAbsolutePath());
////              File souce = jf.getSelectedFile();
////              File dest = jf2.getSelectedFile();
//            Path ins = Paths.get(in);
//            Path outs = Paths.get(out);
////              Files.copy(ins.toPath(), outs.toPath());
//            OutputStream oStream = Files.newOutputStream(path2, null);
//            Files.copy(ins, oStream);

        }
}
}

0 个答案:

没有答案