/** * get the value in the config.properties * * @param key * @return * @throws Exception */ public synchronized String getValue(String key) { Properties properties = new Properties(); String value = null; try { String filePath = getClass().getClassLoader().getResource(file).getPath(); File configFile = new File(filePath); if (configFile.exists()) { logger.info("config file Path:" + filePath); FileInputStream fis = new FileInputStream(new File(filePath)); properties.load(fis); fis.close(); } else { properties.load(this.getClass().getClassLoader().getResourceAsStream(file)); } value = (String) properties.get(key); } catch (FileNotFoundException e) { logger.error(key + " value cant find in " + file); } catch (IOException e) { logger.error(file + " config file io exception"); } catch (Exception e) { logger.error(file + " config file get value error !"); } finally { if (value == null) { logger.info( "can't get the right value of " + key + " from " + file + " , you will get null ."); } } return value; }
public static Object byteArraytoObject(byte[] bytes) throws Exception { Object o = null; try { ByteArrayInputStream in = new ByteArrayInputStream(bytes); ObjectInputStream oin; oin = new ObjectInputStream(in); o = oin.readObject(); oin.close(); in.close(); } catch (Exception e) { logger.error("can't covert these bytes to Object, please check the input !"); throw e; } return o; }
public static byte[] getBytes(Object obj) throws Exception { byte[] arr = null; try { ByteArrayOutputStream bout = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bout); out.writeObject(obj); out.flush(); arr = bout.toByteArray(); out.close(); bout.close(); } catch (Exception e) { logger.error("can't covert this Object to bytes, please check the input !"); throw e; } return arr; }