java 读取list文本_【java基础】读取本地文件赋给Bean或list、Map
private Map messageTypeControllerMap; private static final String CONTROLLERS_CONFIG_PATH = "config/controller/controllers.json"; /** * 從文件中獲取 controller的配置:優(yōu)先級、消息類型、類名 */ public void init(ApplicationContext app) { this.messageTypeControllerMap = new HashMap(); try { String json = FileUtils.readFileToString(new File(CONTROLLERS_CONFIG_PATH));//gson很方便 List> controllerConfigList = GSON.fromJson(json, new TypeToken>>() { }.getType()); for (Map map : controllerConfigList) {//遍歷文件,取值 String messageType = map.get("messageType"); int priority = Integer.parseInt(map.get("priority")); String className = map.get("className"); if (null == messageType || null == className) { throw new Exception("字段缺失!"); } Class> cls = Class.forName(className); AbstractController controller = (AbstractController) app.getBean(cls); controller.setPriority(priority); controller.setMessageType(messageType); this.messageTypeControllerMap.put(messageType, controller); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } }
文件輸入、輸出流:
https://blog.csdn.net/caixiexin/article/details/6719407
Properties:
讀取java配置文件,繼承自hashtable
方法:
getProperty(String key)通過key得到value
load(InputStream inStream)從輸入流中讀取屬性列表(鍵和元素對)
setProperty(String key,String value)調(diào)用hashtable的put方法,設(shè)置鍵值對
store(OutputStream out,String comments)將鍵值對寫入到指定文件中
clear()清除裝載的鍵值對
https://www.cnblogs.com/bakari/p/3562244.html
1/InputStream in = new BufferedInputStream(new FileInputStream(name)); Properties p= new Properties(); p.load(in); 2/ResourceBundle rb = ResourceBundle.getBundle(name,Local.getDefault()); 3/InputStream in = new BufferedInputStream(new FileInputStream(name)); ResourceBundle rb = new PropertyResourceBundle(in); 4/InputStream in = JProperties.class.getResourceAsStream(name); Properties p = new Properties(); p.load(in); 5/InputStream in = JProperties.class.getClassLoader().getResourceAsStream(name); Properties p = new Properties(); p.load(in); 6/InputStream in = ClassLoader.getSystemResourceAsStream(name); Properties p =new Properties(); p.load(in); 7/javax.servlet.ServletContext的getResourceAsStream()方法 示例:InputStream in = context.getResourceAsStream(path); Properties p = new Properties(); p.load(in);
http://www.runoob.com/java/java-properties-class.html
利用GSON
public void init() { this.properties = new Properties(); try { this.properties.load(new FileInputStream(APPLICATION_PROPERTIES_PATH));//路徑 } catch (IOException e) { e.printStackTrace(); GiskaLogger.log(null, GiskaLogger.EXCEPTION, this.getClass().getName() + ":init", "加載文件[" + APPLICATION_PROPERTIES_PATH + "]異常[" + e + "]."); System.exit(-1); } } String json = FileUtils.readFileToString(new File(ROLE_OPERATIONS_PATH)); this.roleOperationListMap = Constants.SINGLE_GSON.fromJson(json, new TypeToken>>() { }.getType()); String platformJson = FileUtils.readFileToString(new File(PLATFORMS_PATH)); this.namePlatformMap = Constants.SINGLE_GSON.fromJson(platformJson, new TypeToken>() { }.getType());
總結(jié)
以上是生活随笔為你收集整理的java 读取list文本_【java基础】读取本地文件赋给Bean或list、Map的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 国内首款量产氢燃料电池轿车 长安深蓝SL
- 下一篇: 那些地方会用C语言多线程,如何用C语言实