package com.wsd;import redis.clients.jedis.Jedis;import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;public class Redis {public static void main(String[] args) {//读取properties fileString fileName = "redis.properties";Map<String,String> map = getProperties(fileName);String host = map.get("host");int port = Integer.valueOf( map.get("port") );String auth = map.get("auth");int select = Integer.valueOf( map.get("select") );//连接redisJedis jedis = new Jedis(host, port);//设置密码jedis.auth(auth);//选择库jedis.select(select);//执行set命令String OK = jedis.set("name","罗小黑");System.out.println(OK);//执行get命令String name = jedis.get("name");System.out.println("name=" + name);if(jedis != null)jedis.close();}private static Map<String,String> getProperties(String fileName){Map<String,String> map = new HashMap<>();Properties properties = new Properties();InputStream inputStream = null;try{//读取properties fileinputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);properties.load(inputStream);String host = properties.getProperty("host");String port = properties.getProperty("port");String auth = properties.getProperty("auth");String select = properties.getProperty("select");map.put("host",host);map.put("port",port);map.put("auth",auth);map.put("select",select);}catch (IOException ex){System.out.println("read file:" + fileName + " fail");}finally {if(inputStream != null);try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}return map;}}