import java.io.*;
import java.nio.file.Path;

public class Main {
    static StringBuilder stringBuilder;
    //包目录，就是你存放的文件目录
    /**
     *  比如我的包路径是
     *  - test
     *  --- usr
     *  ------ bin
     *  --- home
     *
     * 那么目录就填写 xxxx\\test
     */
    static String PATH = "D:\\ZT工作台\\linux\\centos";
    //这个是INSTALL.TXT 生成目录
    static String INSTALL_PATH = PATH + "\\INSTALL.txt";
    public static void main(String[] args) {
        //包目录，就是你存放的文件目录
        File file = new File(PATH);
        stringBuilder = new StringBuilder();
        func(file);
        String s = stringBuilder.toString();

        setFileString(new File(INSTALL_PATH), s);
       //System.out.println(file.getAbsolutePath());
    }

    //获取当前所有文件目录路径
    private static void func(File file){
        File[] fs = file.listFiles();
        File file1 = new File(PATH);
        String runAbsolutePath = file1.getAbsolutePath();

        for(File f:fs){
            if(f.isDirectory()) {
                //若是目录，则递归打印该目录下的文件
                String replace = f.getAbsolutePath().replace(runAbsolutePath, "").replace("\\" ,"/");
                stringBuilder.append(replace).append("->").append(replace).append("->").append("\n");
                func(f);
            }
            if(f.isFile())	{
               //若是文件，直接打印 # 安装filebrowser....
                //bin/ztfilebrowser->usr/bin/ztfilebrowser->511
                //处理当前file的路径
                String replace = f.getAbsolutePath().replace(runAbsolutePath, "").replace("\\" ,"/");
                if (replace.startsWith("usr/bin") ||
                        replace.startsWith("/usr/bin") ||
                        replace.contains("\\lib\\mysql") ||
                        replace.contains("\\usr\\share") ||
                        replace.contains("/usr/share") ||
                        replace.contains("/lib/mysql") ) {
                    stringBuilder.append(replace).append("->").append(replace).append("->").append("511").append("\n");
                } else {
                    stringBuilder.append(replace).append("->").append(replace).append("->").append("511").append("\n");
                }
            }
        }
      //  System.out.println(stringBuilder.toString());
    }



    //写出到文件 INSTALL
    public static boolean setFileString(File fileString, String msg){
        try {
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(fileString)));
            printWriter.print(msg);
            printWriter.flush();
            printWriter.close();
            //  Toast.makeText(getContext(), "OK", Toast.LENGTH_SHORT).show();
            return true;
        } catch (FileNotFoundException e) {
            //  Toast.makeText(getContext(), "Error", Toast.LENGTH_SHORT).show();
            e.printStackTrace();
            return false;
        }
    }

    public static boolean isSymlink(File file) throws IOException {
        if (file == null) {
            throw new NullPointerException("File must not be null");
        }
        File canon;
        if (file.getParent() == null) {
            canon = file;
        } else {
            File canonDir = file.getParentFile().getCanonicalFile();
            canon = new File(canonDir, file.getName());
        }
        return !canon.getCanonicalFile().equals(canon.getAbsoluteFile());
    }
}
