对properties文件的操作

在做程序时,往往会有一些固定的或长期的值,这些值或许在将来才会被更改。

    由于程序完成时,把这些值写在代码,将来更改起不方便。(而且容易忘记在代码中什么地方,哪个文件中时)所以,我们建议将这些值写入配置文件中。

    今天就让我们一起来看看,在 JAVA 中对 properties 这种配置文件的操作吧。

1、properties 文件

#ParseZUrlNum.properties
#Tue Nov 15 11:52:15 CST 2005
nextnum=360
firstnum=73
secondnum=72

<script type="text/javascript"> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script>

2、读写文件

{

ParseNZ pnz = new ParseNZ();

Properties properties = new Properties();

try {

            File f = new File("ParseZUrlNum.properties");
            properties = pnz.getProperties ("e:/work/product/src/com/yesky/productattribute/p/z/model/ParseZUrlNum.properties");
} catch (Exception e)
{
            e.printStackTrace();
}
String firstnum = properties.getProperty("firstnum");
String secondnum = properties.getProperty("secondnum");
String nextnum = properties.getProperty("nextnum");

int first=Integer.parseInt(firstnum);
int second=Integer.parseInt(secondnum);
int next=Integer.parseInt(nextnum);

System.out.println("firstnum=" + firstnum);
System.out.println("secondnum=" + secondnum);
System.out.println("nextnum=" + nextnum);

pnz.setProperties(first,second,next);

}

3、ParseNZ 类

public class ParseNZ{
    Properties p = new Properties();

    public Properties getProperties(String filename) throws IOException {

        ClassLoader cl = this.getClass().getClassLoader();
        FileInputStream input;
        // if(cl!=null)
        //input=cl.getResourceAsStream(filename);
        input = new FileInputStream(filename);
        //else
        //input=ClassLoader.getSystemResourceAsStream(filename);

        p.load(input);
        return p;

    }

   

    public void setProperties(int first, int second, int next) {
        p.setProperty("firstnum",String.valueOf(first));
        p.setProperty("secondnum",String.valueOf(second));
        p.setProperty("nextnum",String.valueOf(next));

        File file = new File ("e:/work/product/src/com/yesky/productattribute/p/z/model/ParseZUrlNum.properties");
        try {
              OutputStream fos = new FileOutputStream(file);
              p.store(fos, "ParseZUrlNum.properties");
              fos.close();
        } catch (FileNotFoundException ex) {
            System.out.println("file is NULL !!!");
              ex.printStackTrace();
        } catch (IOException ex) {
            System.out.println("IO is Error !!!");
              ex.printStackTrace();
        }
    }

}

本例中地址是写死的。可改为如: File file = new File(System.getProperty("user.dir")
                                 + "/EMweb/WEB-INF/admininfo.properties");

这里面的关键是System.getProperty("user.dir") 返回当前工作目录。

 

一个小例子

/**
 * AWT Sample application
 *
 * @author 
 * @version 1.00 07/01/06
 */
 import java.util.Properties;
 import java.io.*;
public class PropertyTest 
{
    
    public static void main(String[] args) 
    {
     //属性对象settings存储了一系列的键和值信息
        Properties settings=new Properties();
        //键和值信息来源于某一文件,故要关联此文件
        try{
          settings.load(new FileInputStream("count.txt"));
         }catch(Exception e)
         {
          settings.setProperty("count",String.valueOf(0));
         }
        //然后打印出信息 
        int c=Integer.parseInt(settings.getProperty("count"))+1;//count 是count.txt中的关键字
        System.out.println("这是第"+c+"次运行");
        
        //然后将次数保存起来,以备下次用
        //保存在了属性Properties对象settings中
        settings.setProperty("count",new Integer(c).toString());
        //为了数据的持久性,然后将其保存在文件中
        try{
         settings.store(new FileOutputStream("count.txt"),"Application has been used:");
        }catch(IOException e)
        {
         e.printStackTrace();
        }
        
    }
}
说明:其实,市面上的一些付费软件,用的有免费试用期。当用户的试用期到期后,会提示你要付费,否则不能再继续使用。可以实现记录用户的使用次数的功能类似于Properties。只不过记录使用次数的文件信息被开放人员隐藏了,比如写到了注册表中等。只要你找到后,将其删除,那恭喜你,还可以继续使用哈。

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 要在Eclipse中创建一个Properties文件,可以按照以下步骤进行操作: 1. 打开Eclipse并创建一个新项目或打开现有项目。 2. 在项目导航器中右键单击所选的项目或所选的文件夹,然后选择“New” > “File”(或者可以直接按Ctrl+N)。 3. 在“New File”对话框中输入文件名,包括扩展名“.properties”(例如“example.properties”),然后单击“Finish”(或者可以按Enter键)。 4. 在创建的Properties文件中添加键值对,格式为“key=value”,每个键值对占据一行。 5. 保存Properties文件,可以按Ctrl+S或者点击工具栏上的“Save”按钮。 完成以上步骤后,您就可以在Eclipse中创建和编辑Properties文件了。 ### 回答2: Eclipse是一款功能强大而且广泛应用于Java开发的集成开发环境。在使用Eclipse开发Java应用的过程中,我们可能需要创建.properties文件,这个文件通常用于存储一些配置信息等,这样我们就可以在应用程序中灵活的调用这些信息,从而更好的满足应用的需求。 在Eclipse中创建.properties文件十分简单,下面以步骤的形式介绍具体操作: 1. 打开Eclipse IDE,进入项目的src目录,右键单击鼠标,选择New -> File。 2. 在弹出的“New File”窗口中,我们可以命名.properties文件的名称。例如我们可以写文件名为:config.properties作为配置文件。 3. 接下来需要在filename.properties文件中添加一些配置信息,例如: db.username = root db.password = 123456 db.url = jdbc:mysql://localhost:3306/sample 4. 保存文件后,我们就可以在Java代码中灵活调用配置信息了。例如,我们可以通过下面的代码来获取数据库的URL地址: Properties prop = new Properties(); prop.load(new FileInputStream("config.properties")); String url = prop.getProperty("db.url"); 通过以上的步骤,我们就可以在Eclipse中创建并使用.properties文件了。总结一下,创建.properties文件分为如下三个步骤:命名.properties文件、添加配置信息、在Java代码中调用配置信息。这种方式灵活、高效,在Java开发中得到广泛应用。 ### 回答3: Eclipse是一款强大的集成开发环境,支持多种编程语言的开发。在Java开发中,经常需要用到properties文件来存储一些配置信息。下面介绍如何在Eclipse中创建properties文件。 1. 新建properties文件 在Eclipse工作区中,右键点击项目或者目录,选择New -> File,弹出新建文件对话框,在文件名后缀中输入.properties,然后点击Finish按钮,即可创建一个新的properties文件。 2. 写入配置信息 在打开的properties文件中,输入需要的配置信息,格式为key=value,每个配置信息占一行,注意不要有空格,配置信息可以用中文或英文字母。 例如,我们可以创建一个jdbc.properties文件,用来存储数据库连接的配置信息: ``` driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/test username=root password=123456 ``` 3. 使用配置信息 在Java程序中,可以使用Properties类来读取properties文件中的配置信息。下面是一个示例程序: ```java import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class Test { public static void main(String[] args) throws IOException { Properties prop = new Properties(); InputStream in = new FileInputStream("jdbc.properties"); prop.load(in); String driver = prop.getProperty("driver"); String url = prop.getProperty("url"); String username = prop.getProperty("username"); String password = prop.getProperty("password"); System.out.println(driver); System.out.println(url); System.out.println(username); System.out.println(password); } } ``` 以上就是在Eclipse中创建properties文件的方法,通过Java程序,可以读取properties文件中的配置信息。properties文件的应用非常广泛,例如Spring、Mybatis等框架都会使用properties文件来进行配置。掌握使用properties文件的方法,对于Java开发是非常重要的。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值