「java属性文件」JAVA文件类型

博主:adminadmin 2022-11-23 08:34:06 58

本篇文章给大家谈谈java属性文件,以及JAVA文件类型对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java中的属性(properties)文件怎么新建

java.util.Properties

类中有方法

Object

setProperty(String key,

String value)

Calls the Hashtable method put.

void

store(OutputStream out,

String comments)

Writes this property list (key and element pairs) in this

Properties table to the output stream in a format suitable

for loading into a Properties table using the

load(InputStream) method.

void

store(Writer writer,

String comments)

Writes this property list (key and element pairs) in this

Properties table to the output character stream in a

format suitable for using the load(Reader)

method.

void

storeToXML(OutputStream os,

String comment)

Emits an XML document representing all of the properties contained

in this table.

void

storeToXML(OutputStream os,

String comment,

String encoding)

Emits an XML document representing all of the properties contained

in this table, using the specified encoding.

怎样使用配置文件设置Java系统属性

右击桌面上“计算机”图标,从弹出的快捷菜单中选择“属性”,打开“系统”窗口。

配置Java的系统环境变量

在打开的“系统”窗口中单击左侧“高级系统设置”选项。

配置Java的系统环境变量

在弹出的“系统属性”对话框中,切换到“高级”选项卡,单击“环境变量”按钮。

配置Java的系统环境变量

4

在弹出的“环境变量”对话框中,在“系统变量”栏中选择“Path”选项,然后单击“编辑”按钮。

配置Java的系统环境变量

配置文件在java 中怎么创建

1.一般在scr下面新建一个属性文件*.properties,如a.properties

然后在Java程序中读取或操作这个属性文件。

代码实例

属性文件a.properties如下:

name=root

pass=liu

key=value

读取a.properties属性列表,与生成属性文件b.properties。代码如下:

1 import java.io.BufferedInputStream;

2 import java.io.FileInputStream;

3 import java.io.FileOutputStream;

4 import java.io.InputStream;

5 import java.util.Iterator;

6 import java.util.Properties;

7

8 public class PropertyTest {

9 public static void main(String[] args) {

10 Properties prop = new Properties();

11 try{

12 //读取属性文件a.properties

13 InputStream in = new BufferedInputStream (new FileInputStream("a.properties"));

14 prop.load(in); ///加载属性列表

15 IteratorString it=prop.stringPropertyNames().iterator();

16 while(it.hasNext()){

17 String key=it.next();

18 System.out.println(key+":"+prop.getProperty(key));

19 }

20 in.close();

21

22 ///保存属性到b.properties文件

23 FileOutputStream oFile = new FileOutputStream("b.properties", true);//true表示追加打开

24 prop.setProperty("phone", "10086");

25 prop.store(oFile, "The New properties file");

26 oFile.close();

27 }

28 catch(Exception e){

29 System.out.println(e);

30 }

31 }

32 }

getProperty/setProperty这两个方法是分别是获取和设置属性信息。

Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集。不过Properties有特殊的地方,就是它的键和值都是字符串类型。

*.properties文件的注释用#。

配置数据的时候是以键值对的形式,调用的时候和修改的时候也是操作键值对。

2.当然还可以用*.xml来配置,位置一般在一个包下面。

例如com.styspace包下面的config.properties文件。

xml version="1.0" encoding="gbk"?

Accounts

Account type="by0003"

code100001/code

pass123/pass

name李四/name

money1000000.00/money

/Account

/Accounts

现在操作config.properties文件。

import org.apache.commons.configuration.Configuration;

import org.apache.commons.configuration.ConfigurationException;

import org.apache.commons.configuration.PropertiesConfiguration;

public class peropertiesLoaderTest {

public static void main(String[] args) throws ConfigurationException{

Configuration config = new PropertiesConfiguration("com/styspace/config.properties");

String name = config.getString("name");

System.out.println("name:" + name);

}

}

java中属性文件中配置的参数如何获取值,求高手

你说的属性文件是.properties文件吧

要用到类java.util.ResourceBundle

ResourceBundle resource=ResourceBundle.getBundle("属性文件名不带扩展名");

需要获取属性的时候调用

resource.getString(key);

就可以了

看Java怎样使用静态块读取属性文件代码

private static String driver =null;

private static String url = null;

private static String user = null;

private static String password = null;

private static BasicDataSource ds;

static{

//读取程序外的.properties 文件

//需要.properties文件的包路径

Properties props = new Properties();

try {

String path ="db.properties";

props.load(

DBUtils.class.getResourceAsStream(path)

);

//properties对象.getProperty("字符串")

driver=props.getProperty("driver");

url=props.getProperty("url");

user=props.getProperty("user");

password=props.getProperty("password");

ds = new BasicDataSource();

ds.setDriverClassName(driver);

ds.setUrl(url);

ds.setUsername(user);

ds.setPassword(password);

Class.forName(driver);

} catch (Exception e) {

e.printStackTrace();

}

}

这是一个JDBC读取配置文件连接数据库的示例代码,供参考!

关于java属性文件和JAVA文件类型的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。

The End

发布于:2022-11-23,除非注明,否则均为首码项目网原创文章,转载请注明出处。