package com.dgt.graphdemo;

import com.dgt.graphdemo.utils.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

import com.mchange.io.FileUtils;

/**
 * @author lugela
 * @Title: JsonFileRunner
 * @ProjectName graphdemo
 * @Description: TODO
 * @date 2019/6/25 13:51
 */
@Slf4j
public class JsonFileRunner implements CommandLineRunner {
    @Override
    public void run(String... args) throws Exception {
        readfile("C:\\Users\\lugela\\Desktop\\jsonfile");
    }


    public  boolean readfile(String filepath) throws FileNotFoundException, IOException {
        try {

            File file = new File(filepath);
            if (!file.isDirectory()) {
                read( filepath, file.getPath());
            } else if (file.isDirectory()) {
                String[] filelist = file.list();
                for (int i = 0; i < filelist.length; i++) {
                    //linux 路径
                    //File readfile = new File(filepath + "/" + filelist[i]);
                    //Windows 本地路径
                    File readfile = new File(filepath + "\\" + filelist[i]);
                    if (!readfile.isDirectory()) {
                        read( readfile.getPath(), readfile.getName());

                    } else if (readfile.isDirectory()) {
                        //  "\\"windows "/"linux 路径
                        readfile(filepath + "\\" + filelist[i]);
                    }
                }

            }

        } catch (FileNotFoundException e) {
            log.info(e.getMessage());
        }
        return true;
    }


    private  void read(String filepath,String filename){
        File file = new File(filepath);
        try {
            String content= FileUtils.getContentsAsString(file);
            if (!StringUtils.isEmpty(filename) &&!StringUtils.isEmpty(filepath)){
                Constants.JSONFILEMAP.put(filename,content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
