getResourceAsStream用法

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;
     }
}

文件结构



Java code ?
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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值