package com.duowenlvshi.duowenlszp.app.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

/**
 * @author yang fang yu
 * @since 2019/8/28 18:05
 */
@Service
public class RedisUtill {
    @Autowired
    private StringRedisTemplate redisTemplate;



    public  void RedisRun() {
        String str = TxtToString("C:\\Users\\Administrator\\Desktop\\code.txt");
        String[] strArray = str.split(",");
        for (String s : strArray) {
            String[] strArray1 = s.split(":");
            redisTemplate.opsForValue().set(strArray1[0],strArray1[1]);
        }
    }

    public String TxtToString(String path){
        String str = null;
        File file = new File(path);
        //判断文件存在并且是文件
        Boolean boo = file.exists()&&file.isFile();
        System.out.println(boo);
        if (boo) {
            BufferedReader bufferedReader = null;
            try {
                //构造一个BufferedReader类来读取文件
                bufferedReader = new BufferedReader(new FileReader(file));
                String linetxt = null;
                //result用来存储文件内容
                StringBuilder result = new StringBuilder();
                //按使用readLine方法，一次读一行
                while ((linetxt = bufferedReader.readLine()) != null) {
                    result.append(linetxt);
                }
                str = result.toString();
            } catch (Exception e) {
                System.out.println("读取文件内容出错");
                e.printStackTrace();
            }finally {
                try {
                    bufferedReader.close();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        }else{
            System.out.println("找不到指定的文件");
        }
        return str;
    }

    public void test2(){
      /*  redisTemplate.opsForValue().set("sex1","不限");*/

    }


}
