获取文本(txt) 键值对

text:

key=value

key=value

key=value

key=value

类:

  1 package com.holike.crm.mall.tools;
  2
  3 import java.io.File;
  4 import java.util.concurrent.ConcurrentHashMap;
  5 public class PropUtil {
  6     /**
  7      * PropKit. PropKit can load properties file from CLASSPATH or File object.
  8      */
  9
 10
 11         private static Prop prop = null;
 12         private static final ConcurrentHashMap<String, Prop> map = new ConcurrentHashMap<String, Prop>();
 13
 14         private PropUtil() {}
 15
 16         /**
 17          * Using the properties file. It will loading the properties file if not loading.
 18          * @see #use(String, String)
 19          */
 20         public static Prop use(String fileName) {
 21             return use(fileName, "utf-8");
 22         }
 23
 24         /**
 25          * Using the properties file. It will loading the properties file if not loading.
 26          * <p>
 27          * Example:<br>
 28          * PropKit.use("config.txt", "UTF-8");<br>
 29          * PropKit.use("other_config.txt", "UTF-8");<br><br>
 30          * String userName = PropKit.get("userName");<br>
 31          * String password = PropKit.get("password");<br><br>
 32          *
 33          * userName = PropKit.use("other_config.txt").get("userName");<br>
 34          * password = PropKit.use("other_config.txt").get("password");<br><br>
 35          *
 36          * PropKit.use("com/jfinal/config_in_sub_directory_of_classpath.txt");
 37          *
 38          * @param fileName the properties file‘s name in classpath or the sub directory of classpath
 39          * @param encoding the encoding
 40          */
 41         public static Prop use(String fileName, String encoding) {
 42             Prop result = map.get(fileName);
 43             if (result == null) {
 44                 result = new Prop(fileName, encoding);
 45                 map.put(fileName, result);
 46                 if (PropUtil.prop == null)
 47                     PropUtil.prop = result;
 48             }
 49             return result;
 50         }
 51
 52         /**
 53          * Using the properties file bye File object. It will loading the properties file if not loading.
 54          * @see #use(File, String)
 55          */
 56         public static Prop use(File file) {
 57             return use(file, "utf-8");
 58         }
 59
 60         /**
 61          * Using the properties file bye File object. It will loading the properties file if not loading.
 62          * <p>
 63          * Example:<br>
 64          * PropKit.use(new File("/var/config/my_config.txt"), "UTF-8");<br>
 65          * Strig userName = PropKit.use("my_config.txt").get("userName");
 66          *
 67          * @param file the properties File object
 68          * @param encoding the encoding
 69          */
 70         public static Prop use(File file, String encoding) {
 71             Prop result = map.get(file.getName());
 72             if (result == null) {
 73                 result = new Prop(file, encoding);
 74                 map.put(file.getName(), result);
 75                 if (PropUtil.prop == null)
 76                     PropUtil.prop = result;
 77             }
 78             return result;
 79         }
 80
 81         public static Prop useless(String fileName) {
 82             Prop previous = map.remove(fileName);
 83             if (PropUtil.prop == previous)
 84                 PropUtil.prop = null;
 85             return previous;
 86         }
 87
 88         public static void clear() {
 89             prop = null;
 90             map.clear();
 91         }
 92
 93         public static Prop getProp() {
 94             if (prop == null)
 95                 throw new IllegalStateException("Load propties file by invoking PropKit.use(String fileName) method first.");
 96             return prop;
 97         }
 98
 99         public static Prop getProp(String fileName) {
100             return map.get(fileName);
101         }
102
103         public static String get(String key) {
104             return getProp().get(key);
105         }
106
107         public static String get(String key, String defaultValue) {
108             return getProp().get(key, defaultValue);
109         }
110
111         public static Integer getInt(String key) {
112             return getProp().getInt(key);
113         }
114
115         public static Integer getInt(String key, Integer defaultValue) {
116             return getProp().getInt(key, defaultValue);
117         }
118
119         public static Long getLong(String key) {
120             return getProp().getLong(key);
121         }
122
123         public static Long getLong(String key, Long defaultValue) {
124             return getProp().getLong(key, defaultValue);
125         }
126
127         public static Boolean getBoolean(String key) {
128             return getProp().getBoolean(key);
129         }
130
131         public static Boolean getBoolean(String key, Boolean defaultValue) {
132             return getProp().getBoolean(key, defaultValue);
133         }
134
135         public static boolean containsKey(String key) {
136             return getProp().containsKey(key);
137         }
138
139 }

PropUtil.java

  1 package com.holike.crm.mall.tools;
  2
  3 import java.io.File;
  4 import java.io.FileInputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.InputStreamReader;
  8 import java.util.Properties;
  9
 10 /**
 11  * Prop. Prop can load properties file from CLASSPATH or File object.
 12  */
 13 public class Prop {
 14     private Properties properties = null;
 15
 16     /**
 17      * Prop constructor.
 18      *
 19      * @see #Prop(String, String)
 20      */
 21     public Prop(String fileName) {
 22         this(fileName, "utf-8");
 23     }
 24
 25     /**
 26      * Prop constructor
 27      * <p>
 28      * Example:<br>
 29      * Prop prop = new Prop("my_config.txt", "UTF-8");<br>
 30      * String userName = prop.get("userName");<br>
 31      * <br>
 32      *
 33      * prop = new Prop("com/jfinal/file_in_sub_path_of_classpath.txt",
 34      * "UTF-8");<br>
 35      * String value = prop.get("key");
 36      *
 37      * @param fileName
 38      *            the properties file‘s name in classpath or the sub directory
 39      *            of classpath
 40      * @param encoding
 41      *            the encoding
 42      */
 43     public Prop(String fileName, String encoding) {
 44         InputStream inputStream = null;
 45         try {
 46             inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName); // properties.load(Prop.class.getResourceAsStream(fileName));
 47             if (inputStream == null)
 48                 throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
 49             properties = new Properties();
 50             properties.load(new InputStreamReader(inputStream, encoding));
 51         } catch (IOException e) {
 52             throw new RuntimeException("Error loading properties file.", e);
 53         } finally {
 54             if (inputStream != null)
 55                 try {
 56                     inputStream.close();
 57                 } catch (IOException e) {
 58 //                    LogKit.error(e.getMessage(), e);
 59                     e.printStackTrace();
 60                 }
 61         }
 62     }
 63
 64     /**
 65      * Prop constructor.
 66      *
 67      * @see #Prop(File, String)
 68      */
 69     public Prop(File file) {
 70         this(file, "utf-8");
 71     }
 72
 73     /**
 74      * Prop constructor
 75      * <p>
 76      * Example:<br>
 77      * Prop prop = new Prop(new File("/var/config/my_config.txt"), "UTF-8");<br>
 78      * String userName = prop.get("userName");
 79      *
 80      * @param file
 81      *            the properties File object
 82      * @param encoding
 83      *            the encoding
 84      */
 85     public Prop(File file, String encoding) {
 86         if (file == null)
 87             throw new IllegalArgumentException("File can not be null.");
 88         if (file.isFile() == false)
 89             throw new IllegalArgumentException("File not found : " + file.getName());
 90
 91         InputStream inputStream = null;
 92         try {
 93             inputStream = new FileInputStream(file);
 94             properties = new Properties();
 95             properties.load(new InputStreamReader(inputStream, encoding));
 96         } catch (IOException e) {
 97             throw new RuntimeException("Error loading properties file.", e);
 98         } finally {
 99             if (inputStream != null)
100                 try {
101                     inputStream.close();
102                 } catch (IOException e) {
103 //                    LogKit.error(e.getMessage(), e);
104                     e.printStackTrace();
105                 }
106         }
107     }
108
109     public String get(String key) {
110         return properties.getProperty(key);
111     }
112
113     public String get(String key, String defaultValue) {
114         return properties.getProperty(key, defaultValue);
115     }
116
117     public Integer getInt(String key) {
118         return getInt(key, null);
119     }
120
121     public Integer getInt(String key, Integer defaultValue) {
122         String value = properties.getProperty(key);
123         if (value != null)
124             return Integer.parseInt(value.trim());
125         return defaultValue;
126     }
127
128     public Long getLong(String key) {
129         return getLong(key, null);
130     }
131
132     public Long getLong(String key, Long defaultValue) {
133         String value = properties.getProperty(key);
134         if (value != null)
135             return Long.parseLong(value.trim());
136         return defaultValue;
137     }
138
139     public Boolean getBoolean(String key) {
140         return getBoolean(key, null);
141     }
142
143     public Boolean getBoolean(String key, Boolean defaultValue) {
144         String value = properties.getProperty(key);
145         if (value != null) {
146             value = value.toLowerCase().trim();
147             if ("true".equals(value))
148                 return true;
149             else if ("false".equals(value))
150                 return false;
151             throw new RuntimeException("The value can not parse to Boolean : " + value);
152         }
153         return defaultValue;
154     }
155
156     public boolean containsKey(String key) {
157         return properties.containsKey(key);
158     }
159
160     public Properties getProperties() {
161         return properties;
162     }
163 }

Prop.java

调用:

  

Prop prop= PropUtil.use("file.txt");
prop.get("key");

时间: 2024-07-29 23:13:53

获取文本(txt) 键值对的相关文章

js/jquery获取文本框的值与改变文本框的值

我们就用它来学习获取文本框的值及改变文本框的值. 代码如下 复制代码 <script>function get1(){ document.getElementById("txtbox2").value=document.getElementById("txtbox").value; //获取文本框1的值,并赋值给文本框2}</script> <table width="500" border="0"

React-Native获取文本框的值

要想获取文本框的值,首先我们需要看一下官方文档的解释: 这里的意思是说当文本框的内容改变的时候,文本框的输入的内容就会作为一个参数进行传递.因此我们就可以获取到文本框里面的内容就好了. 1 constructor (props) { 2 super (props) 3 this.state = { 4 screen: this.initScreen(), 5 txtValue: null, 6 dataSource: new ListView.DataSource({ 7 rowHasChang

jquery设置文本框值 与获取文本框的值

<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> <script src="../js/jquery-1.12.4.js"></script> </head> <body> <input type="tex

获取当前 系统时间 + 获取当前url 键值;

一://系统当前时间 function show(){ var mydate = new Date(); var str = "" + mydate.getFullYear() + "年"; str += (mydate.getMonth()+1) + "月"; str += mydate.getDate() + "日"; str+=mydate.getHours() + "时"; str+=mydate.

Py3+PyQt5+Eric6:学习记录之第一天:点击按钮获取文本框的值并输出。

一.使用qt designer拖拽界面. 使用qtdesigner拖拽界面: 如图左侧导航栏,1:Sources,2:Forms,3:Resouces 1:代码,2:Qt Designer的拖拽界面的代码,xml形式的 3:暂未使用,猜测用来放置一些图片.音频.视频之类的. 在2:Forms中右键点击,然后选择new form,选择 类型:Dialog然后会启动Qt Designer,拖拽出如图的界面. 二.在qt designer中  自定义信号/槽 在Qt Designer的界面中 1:编辑

Properties文件工具类的使用--获取所有的键值、删除键、更新键等操作

有时候我们希望处理properties文件,properties文件是键值对的文件形式,我们可以借助Properties类操作. 工具类如下:(代码中日志采用了slf4j日志) package cn.xm.exam.utils; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java

smarty中ifelse、foreach以及获取数组中键值名的一个实例

<{if empty($history)}> <tr> <td colspan="6">Not any records!</td> </tr> <{else}> <{foreach from=$history item=item}> <tr> <td><{$item.id}></td> <td> <div style="max-

循环获取文本框的值

1 /// <summary> 2 /// 遍历页面textbox 3 /// </summary> 4 private float ForText(string s) {//System.Web.UI.WebControls.TextBox tbxTableMathScore = (System.Web.UI.WebControls.TextBox)gr_Main.Rows[rowIndex].FindControl("labMathScore"); 5 Fi

java 遍历Map并且获取其中的键值

errCodeMap是要遍历的Map, Iterator it = errCodeMap.entrySet().iterator(); while(it.hasNext()){ Map.Entry entry = (Entry) it.next(); String key = entry.getKey().toString(); // 返回的对应map的key值 String value = entry.getValue().toString();  // 返回的对应map的value值 }

jq中获取object 的键值key

unction printObject(obj){ //obj = {"cid":"C0","ctext":"区县"}; var temp = ""; for(var i in obj){//用javascript的for/in循环遍历对象的属性 temp += i+":"+obj[i]+"\n"; } alert(temp);//结果:cid:C0 \n ctext