递归遍历文件中所有html文件保存

owhile 2020-04-30

import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        File file = new File("/Users/mima000000/Desktop/health/讲义");
        te(file);


    }

    public static void te(File file) throws IOException {
        File[] files = file.listFiles();
        for (File file1 : files) {
            if (file1.isFile()){
                if (file1.toString().endsWith(".html")){
                    String name = file1.getName();
                    String resname = name.substring(0, name.lastIndexOf("."));
                    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file1));
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File("/Users/mima000000/Desktop/health/res/"+resname+".html")));
                    int len;
                    byte[] content = new byte[1024*10];
                    while ((len = (bis.read(content)))!= -1){
                        bos.write(content,0,len);
                    }

                }
            }else {
                te(file1);
            }
        }
    }
}

相关推荐