2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;
public
class
Util {
/**
* @param args
*/
public
static
void
main(String[] args) {
new
Util().writeProperties();
}
public
void
writeProperties(){
Properties p =
new
Properties();
p.setProperty(
"name"
,
"Gemerl"
);
p.setProperty(
"password"
,
"123456"
);
File file=
new
File(
"src/properties"
);
InputStream in;
OutputStream os;
if
(!file.exists()){
file.mkdirs();
}
file=
new
File(
"src/properties/test.properties"
);
if
(!file.exists()){
try
{
file.createNewFile();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try
{
in =
new
BufferedInputStream(
new
FileInputStream(file));
p.load(in);
os =
new
FileOutputStream(file);
p.store(os,
"看能写注释不.."
);
in.close();
os.close();
}
catch
(Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
* @return 通过properties的key返回value值
*/
public
String getString(String key){
InputStream in =
this
.getClass().getResourceAsStream(
"src/test.properties"
);
//上面的这句in是null值
Properties props =
new
Properties();
String val=null;
try
{
props.load(in);
val=(String) props.getProperty(key);
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return
val;
}
}
|
文件结构
1
2
3
4
5
6
7
8
9
10
11
12
13
|
File file=
new
File(
"src/properties"
);
if
(!file.exists()){
file.mkdirs();
}
file=
new
File(
"src/properties/test.properties"
);
if
(!file.exists()){
try
{
file.createNewFile();
}
catch
(IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
|
另外,如果我想让properties文件不存在就自动在src下面创建该怎么写的。。
/**
* 初始化方法
*/
@PostConstruct
public void init()
{
Properties properties = new Properties();
InputStream inps = this.getClass().getResourceAsStream( "/solr.properties");
try
{
properties.load(inps);
querySummaryServer = new LBHttpSolrServer();
writeSummaryServer = new LBHttpSolrServer();
writeServer = new LBHttpSolrServer();
if(properties.get(ItemIndexConstant.QUERYSUMMARYSERVER1) != null && StringUtils.hasText(properties.get(ItemIndexConstant.QUERYSUMMARYSERVER1).toString()))
{
querySummaryServer.addSolrServer(properties.get(ItemIndexConstant.QUERYSUMMARYSERVER1).toString());
}
if(properties.get(ItemIndexConstant.QUERYSUMMARYSERVER2) != null && StringUtils.hasText(properties.get(ItemIndexConstant.QUERYSUMMARYSERVER2).toString()))
{
querySummaryServer.addSolrServer(properties.get(ItemIndexConstant.QUERYSUMMARYSERVER2).toString());
}
if(properties.get(ItemIndexConstant.WRITERSUMMARYSERVER) != null && StringUtils.hasText(properties.get(ItemIndexConstant.WRITERSUMMARYSERVER).toString()))
{
writeSummaryServer.addSolrServer(properties.get(ItemIndexConstant.WRITERSUMMARYSERVER).toString());
}
if (properties.get(ItemIndexConstant.WRITERSERVER) != null && StringUtils.hasText(properties.get(ItemIndexConstant.WRITERSERVER).toString()))
{
writeServer.addSolrServer(properties.get(ItemIndexConstant.WRITERSERVER).toString());
}
} catch (IOException e)
{
logger.error("初始化记载查询索引服务器失败!");
e.printStackTrace();
}
logger.info("**************************初始化加载查询索引服务器成功!********************");
}
package com.lvtu.service.api.utils;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import com.lvmama.comm.vo.Constant;
public class HomeSearchConstant {
private static Properties properties;
private static volatile HomeSearchConstant instance=null;
private void init() {
try {
properties = new Properties();
properties.load(getClass().getResourceAsStream("/home_search.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
private HomeSearchConstant(){
init();
}
public static HomeSearchConstant getInstance() {
if (instance == null) {
synchronized (Constant.class) {
if(instance==null){
instance = new HomeSearchConstant();
}
}
}
return instance;
}
/**
* 获取属性
*
* @return
*/
public String getValue(String key) {
return System.getProperty(key) != null?System.getProperty(key):properties.getProperty(key);
}
/**
* 精确品类关键词
* @return
*/
public Map<String, String[]> getValueToPreciseMap(){
Map<String, String[]> map = new HashMap<String, String[]>();
try {
Enumeration<Object> en = properties.keys();
while(en.hasMoreElements()){
String name = en.nextElement().toString();
if(!"generalKeyWords".equals(name) && !"blendedKeyWords".equals(name)){
String value = properties.getProperty(name);
map.put(name, value.split(","));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 普通关键词
* @return
*/
public Map<String, String[]> getValueToGeneralMap(){
Map<String, String[]> map = new HashMap<String, String[]>();
try {
Enumeration<Object> en = properties.keys();
while(en.hasMoreElements()){
String name = en.nextElement().toString();
if("generalKeyWords".equals(name)){
String value = properties.getProperty(name);
map.put(name, value.split(","));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
/**
* 混合关键词
* @return
*/
public Map<String, String[]> getValueToBlendedMap(){
Map<String, String[]> map = new HashMap<String, String[]>();
try {
Enumeration<Object> en = properties.keys();
while(en.hasMoreElements()){
String name = en.nextElement().toString();
if("blendedKeyWords".equals(name)){
String value = properties.getProperty(name);
map.put(name, value.split(","));
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
}