java对properties文件的操作

news/2024/7/5 5:06:11
在做程序时,往往会有一些固定的或长期的值,这些值或许在将来才会被更改。

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

    今天就让我们一起来看看,在 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") 返回当前工作目录。

 

http://www.niftyadmin.cn/n/4413878.html

相关文章

JAVA组件大全 复选框、选项按钮、列表方框、下拉式列表的使用与介绍

7&#xff0d;1&#xff1a;使用JCheckBox组件&#xff1a; 类层次结构图&#xff1a;java.lang.Object--java.awt.Component--java.awt.Container--javax.swing.JComponent--javax.swing.AbstractButton--javax.swing.JToggleButton--javax.swing.JCheckBoxJCheckBox与JRadioB…

JAVA界面组件---swing标签与按钮的使用与介绍

Border,Icon,JLabel,JButton,JToggleButton 6-1:Border的使用 Border类是应用在描绘组件的边界&#xff0c;Border本身是一个interface,里面定义了3个方法&#xff0c;为getBorderInsets()、isBorderOpaque() 、与isBorderOpaque()、与paintBorder().若您想使用Border类来绘制你…

java swing 组件大全----测试Swing所有组件及其相应的事件

import javax.swing.*;import java.awt.*; import java.awt.event.*; import javax.swing.tree.*; import javax.swing.event.*; import javax.swing.border.*; import javax.swing.table.*;/** * Swing 组件测试程序* 测试Swing所有组件及其相应的事件* author 天翼.李 2003.4…

JAVA界面设计大全----JTabbedPane,JScroolPane,JScrolBa的使用

6&#xff0d;1&#xff0d;1&#xff1a;JTabbedPane的使用&#xff1a; 类层次结构图&#xff1a;java.lang.Object--java.awt.Component--java.awt.Container--javax.swing.JComponent--javax.swing.JTabbedPane JTabbedPane构造函数&#xff1a; JTabbedPane():建立一个空的…

在JTextArea中如何一行一行的读据

在JTextArea中如何一行一行的读据在JTextArea中如何一行一行的读据 我的这个JTextArea中每一行都是一个手机号&#xff0c;我怎么把这每一行都保存为一个String 问题点数&#xff1a;0、回复次数&#xff1a;2 回复:在JTextArea中如何一行一行的读据1 呵呵刚刚解决&#xf…

关于请求的转发与重定向的原理以及区别

今天说说请求的转发与重定向 请求的转发与重定向是web应用页面跳转的主要手段&#xff0c;在Web应用中使用非常广泛。所以我们一定要搞清楚他们的区别。 首先看一张图&#xff0c;看看他们之间携带、返回的数据以及在服务器中是怎么跳转的&#xff1a; 下面具体说说请求的转发…

虚拟键盘按键的ASCII码表

常量名称 十六位值 十位值 鼠标或按钮的值 VK_LBUTTON 1 1 鼠标左键钮VK_RBUTTON 2 2 鼠标右键钮VK_CANCEL 3 3 Control-break执行VK_MBUTTON 4 4 鼠标中键钮 05-07 05-07 未定义VK_BACK 8 8 Backspace键VK_TA…

解决乱码问题(请求响应)

今天说说关于请求与响应中的字符编码设置。 1. 字符编码问题的引入 通常web程序在接收请求并处理过程中&#xff0c;如果不注意编码格式及解码格式&#xff0c;很容易导致中文乱码&#xff0c;引起这个问题的原因到底在哪里&#xff1f;如何解决呢&#xff1f; 说到这个问题我…