我现在把程序打成了一个jar包,但程序需要对一个properties文件进行读写操作,这个文件我想放到和这个jar包平行的位置,请问我该怎么在程序里面写这个读写函数,而且这个properties文件该放到什么地方。

解决方案 »

  1.   


    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.util.Properties;public class Config
    {
        private String path;    private Properties config;    public Config(String path)
        {
            this.path = path;
        }    public boolean loadConfig()
        {
            config = new Properties();
            FileInputStream fis = null;
            boolean result = false;
            
            try
            {
                fis = new FileInputStream(path);
                config.load(fis);
                result = true;
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                if(fis != null)
                {
                    try
                    {
                        fis.close();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }        return result;
        }    public void saveConfig()
        {
            FileOutputStream fos = null;        try
            {
                fos = new FileOutputStream(path);
                config.store(fos, "");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                if(fos != null)
                {
                    try
                    {
                        fos.close();
                    }
                    catch(Exception e)
                    {
                        e.printStackTrace();
                    }
                }
            }
        }    public void setPropertie(String key, String value)
        {
            config.setProperty(key, value);
        }    public String getProperty(String key)
        {
            return config.getProperty(key);
        }    public String getPath()
        {
            return path;
        }    public void setPath(String path)
        {
            this.path = path;
        }
    }
    这个类直接拿去用,至于Properties路径嘛,如果是主目录就直接写文件名字好了,否则就写".//目录名//文件名"
      

  2.   

    一个建议,你在你的程序的main函数中  File fl = new File("a.properies");
    然后再 sysout  fl.getAbsolu..(); 看看他的位置,然后你再放到相应的位置看下
    也可以先得到  user.dir  System.getProperty("user.dir");来得到你的目录在哪,然后再确定相关文件的放置
      

  3.   

    1 .把读取PROPERTIES文件的路径写成传参的形式.....

    2. 在程序里用相对路径....
      

  4.   

    craky,我这样子用为什么提示:java.lang.NullPointerException
     public String getPara(String prName) {
            Config con=null;
            con.setPath("config.properties");
            String temp=con.getProperty(prName);
            return temp;
    }
    不知道我这样的用法对不对啊
      

  5.   

    craky,不知道我用你的类按下面那样子写对不对:
     public String getPara(String prName) {
            Config con=null;
            con.setPath("config.properties");
            String temp=con.getProperty(prName);
            return temp;
    }
    但是已经弹出了 java.lang.NullPointerException
      

  6.   

    Config没初始化呀,呵呵
        public String getPara(String prName)
        {
            Config con = new Config("config.properties");
            String temp = con.getProperty(prName);
            return temp;
        }
      

  7.   

    那个setPath的方法没用的,可以删掉如果要用的话你得再写一个无参构造方法,这样就可以按如下的方法用
    Config con = new Config();
    con.setPath("config.properties");
      

  8.   

    之前用的是“/config.properties”相对地址,然后放到bin的下面才能读取,后来用别人的类,使用“../config.properties”路径,放到和bin,src平行的文件夹才能读取,但是我用fat—jar做成jar包后,第二种情况直接就读不出来配置文件,第一种能读出来,但打到包里面去了,无法修改
      

  9.   

    主要是我上面很多地方都用到了setpara函数,删掉重新改工作量太大
      

  10.   

    错了,还少了一句,呵呵,不好意思
        public String getPara(String prName)
        {
            Config con = new Config("config.properties");
            con.loadConfig();
            String temp = con.getProperty(prName);
            return temp;
        }
      

  11.   

    再者我用这个Config con = new Config(); 为什么会提示构造函数未定义?
      

  12.   


    那你给Config里加一个方法    public Config()
        {}
    然后这样用
        public String getPara(String prName)
        {
            Config con = new Config();
            con.setPath("config.properties");
            con.loadConfig();
            String temp = con.getProperty(prName);
            return temp;
        }
      

  13.   

    我根据2,3楼写的测试方法:public class ConfigTest {
    public static void main(String[] args){
    String filepath = System.getProperty("user.dir")+"\\config\\config.properties";
    System.out.println(filepath);

    Config config = new Config(filepath);

    config.loadConfig();
    config.setPropertie("test2", "You are ready");
    config.saveConfig();
    }
    }
      

  14.   


    就跟你的jar包放在同一目录下,然后直接config.properties
      

  15.   

    我的config.properties文件在工程目录下的config文件夹下。
    config.properties由
    变为
      

  16.   

    不过好像回写有点问题,写不进去
     Config con = new Config("config.properties");
            con.loadConfig();
            con.setPropertie(keyName, prName);
      

  17.   

    con.setPropertie(keyName, prName);完了要save一下的 con.saveConfig();参看16楼的,hoho
      

  18.   

    非常感谢,问题以解决,谢谢大家提供帮助,尤其craky
      

  19.   

    呵呵,免礼免礼
    同时BS那个java 2000 net什么的
      

  20.   

    呵呵,免礼免礼
    同时BS那个java 2000 net什么的