javaIo——运用字节流完整复制文件夹及文件

突发奇想要写一段全部复制文件夹的代码,可以复制一个文件夹到任何目录(包括盘符根目录或者文件夹根目录),感觉自己写的很冗余,新手希望大牛指点一下!

public class Test {
    int a = 0;
    static File startPath = null;
    static File endPath = null;
    public static void main(String[] args) throws IOException {
    Scanner input = new Scanner(System.in);
    System.out.println("请输入您要复制的原目录:(完整目录)");
    startPath = new File(input.next());
    System.out.println("请输入您要复制的目标目录:(完整目录)");
    endPath = new File(input.next());
    Test2016510 test2016510 = new Test2016510();
    test2016510.copy(startPath, endPath);
    System.out.println("复制完成");
}
@SuppressWarnings("unused")
public void copy(File startFile, File endFile) throws IOException {

    File startFilePath = startFile;
    File endFilePath = endFile;

    File[] startfilePaths = startFilePath.listFiles();
    if (startFile.exists()) {
        for (File file : startfilePaths) {

            if (file.isDirectory()) {
                if (file != null) {
                    String ss = file.toString().substring(3,
                            file.toString().length());
                    File newFile = new File(endFilePath.toString() + "\\"
                            + ss);
                    newFile.mkdirs();
                    this.copy(file, newFile);
                } else {
                    String ss = file.toString().substring(3,
                            file.toString().length());
                    File newFile = new File(endFilePath.toString() + "\\"
                            + ss);
                    newFile.mkdirs();
                }
            } else if (file.isFile()) {
                String ss = file.getPath().substring(3,
                        file.toString().length());
                File newFile = new File(this.endPath + "\\" + ss);
                String ssss = file.getParent().substring(3);
                File parentFile = new File(this.endPath + "\\" + ssss);
                if (!parentFile.exists()) {
                    parentFile.mkdirs();
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(fis);

                    FileOutputStream fos = new FileOutputStream(newFile);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);

                    byte[] buff = new byte[1024];
                    int temp = 0;
                    while ((temp = bis.read(buff)) != -1) {
                        bos.write(buff, 0, temp);
                        bos.flush();
                    }
                    bos.close();
                    fos.close();
                    bis.close();
                    fis.close();
                } else {
                    FileInputStream fis = new FileInputStream(file);
                    BufferedInputStream bis = new BufferedInputStream(fis);

                    FileOutputStream fos = new FileOutputStream(newFile);
                    BufferedOutputStream bos = new BufferedOutputStream(fos);

                    byte[] buff = new byte[1024];
                    int temp = 0;
                    while ((temp = bis.read(buff)) != -1) {
                        bos.write(buff, 0, temp);
                        bos.flush();
                    }
                    bos.close();
                    fos.close();
                    bis.close();
                    fis.close();
                }

            }
        }
    } else {
        System.out.println("您指定的文件不存在,程序结束。");
    }
}

你可能感兴趣的:(java,IO流)