一、路径获取问题:
①WEB项目获取WEB_INF目录下的文件路径,无论在servlet下还是普通类中都可以用以下方法获取,再拼接。
获取到如下路径: file:/D:/Tools/apache-tomcat-6.0.35/webapps/p_txy/WEB-INF/classes/
String path = Thread.currentThread().getContextClassLoader().getResource("").toString();
path = path.replace("file:/", ""); //去掉file:/
path = path.replace("classes/", ""); //去掉class/
path += "input.txt";
拼接成的路径实例: "D:/Tools/apache-tomcat-6.0.35/webapps/p_txy/WEB-INF/input.txt
②获取类绝对路径可以用如下方式:
getClass().getProtectionDomain().getCodeSource().getLocation().getPath();
获取到如下路径: /D:/Tools/apache-tomcat-6.0.35/webapps/p_txy/WEB-INF/classes/com/montnets/isms/common/ReadData.class
注意: this.getServletContext().getRealPath("/WEB-INF/input.txt");这种方法获取WEB_INF目录下的文件路径只能用在servlet里。
③、java项目
Thread.currentThread().getContextClassLoader().getResource("").toString();
则可以得到类似下面路径。例如:file:/C:/Users/dell/Workspaces/MyEclipse%2010/Test/bin/
二、使用java8,获取集合里面的属性集合:
**方法1:**
//users User实体类对象集合
//User 实体类
//getName 实体类属性的get方法
List<String> names = users.stream().map(User::getName).collect(Collectors.toList())
**方法2:**
//user User实体类对象
//getName 实体类属性的get方法
List<String> names= users.stream().map(user->user.getName()).collect(Collectors.toList());
**方法3:java8以前的用法**
List<String> names=new ArrayList<String>();
//users :用户集合
for(int i=0;i<users.size();i++){
names.add(users.get(i).getName());
}
**结论:以前用法具有相同的作用,只是java8代码显得比较简洁。**
三、ResourceBundle.getBundle读取资源文件(.properties)信息,以key-value方式放到Map中:
Map<String, String> menuMap = new HashMap<String, String>();
ResourceBundle resourceBundle= ResourceBundle.getBundle("xxx.properties");
Enumeration<String> keys = bundle.getKeys();
while (keys.hasMoreElements()){
key = keys.nextElement();
menuMap.put(key, bundle.getString(key));
}
四、获取模板中的参数,把参数放到List<String>中
模板如下:
{
{first.DATA}}发票抬头:{
{keyword1.DATA}}开票金额:{
{keyword2.DATA}}发票类型:{
{keyword3.DATA}}{
{remark.DATA}}
获取参数:first、keyword1、keyword2、keyword3
/**
* 获取模板内容里的参数
* @return
*/
public static