当前位置:首页 » 数据仓库 » java如何读写配置文件
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

java如何读写配置文件

发布时间: 2022-01-11 23:09:59

A. Java中如何设置读取ini配置文件

//读取一般的属性文件
FileInputStreamfin=newFileInputStream("my.ini");//打开文件
Propertiesprops=newProperties();//建立属性类
props.load(fin);//读入文件
fin.close();//关闭文件

B. 怎样读取和写我自己写的一个java配置文件

import java.io.File;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* @Title: JavaBeanCreater.java
* @Package liaodong.xiaoxiao.dom4j.sxd.bean
* @Description: TODO(根据xml配置文件,生成javabean)
* @author 辽东小小
* @date 2012-3-1 上午09:13:25
* @version V1.0
*/
public class JavaBeanCreater
{
public static void main(String[] args)
{
//xml的存放路径
String xmlPath = "D:\\test\\javabean.xml";
File xmlFile = new File(xmlPath);

StringBuffer sBuffer = new StringBuffer("public class ");

SAXReader sReader = new SAXReader();
Document document;
try
{
//读取xml文件,返回document对象
document = sReader.read(xmlFile);
//获取根元素
Element rootElement = document.getRootElement();
//
String className = rootElement.elementText("classname");
sBuffer.append(className).append("\n");
sBuffer.append("{\n");
String nature = rootElement.elementText("nature");
String natures[] = nature.split(",");
//构造成员变量
for (String n : natures)
{
sBuffer.append("private String ").append(n).append(";\n");
}
//构造set/get方法
for (String n : natures)
{
String methodName = n.substring(0, 1).toUpperCase()+n.substring(1);
//拼写set方法

sBuffer.append("public void set").append(methodName).append("( String ").append(n).append(" )\n{");
sBuffer.append("this.").append(n).append(" = ").append(n).append(";\n}\n");
//拼写get方法
sBuffer.append("public String get").append(methodName).append(" ( )\n{\n");
sBuffer.append("return ").append(n).append(";\n}\n");
}

//language 方法跟上面的类似 省略。。。。。。。。

//写文件的方法也省略了。。。。。。。。。
sBuffer.append("}");
System.out.println(sBuffer.toString());
//

}catch(Exception e){
e.printStackTrace();
}
}
}
用Dom4j解析的xml
##############################################################
运行结果为:
public class animal
{
private String name;
private String food;
private String age;
public void setName( String name )
{this.name = name;
}
public String getName ( )
{
return name;
}
public void setFood( String food )
{this.food = food;
}
public String getFood ( )
{
return food;
}
public void setAge( String age )
{this.age = age;
}
public String getAge ( )
{
return age;
}
}

C. Java读写文件的几种方法

java读取配置文件的几种方法如下:
方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
方式二:采用ResourceBundle类读取配置信息,
优点是:可以以完全限定类名的方式加载资源后,直接的读取出来,且可以在非Web应用中读取资源文件。缺点:只能加载类classes下面的资源文件且只能读取.properties文件。

D. java如何读取XML配置文件

学一下jdom吧,很简单的,可以很方便的提取xml的值,网上很多相关教程(推荐张龙的xml培训视频)

E. java中如何从当前项目中读取运行另一项目中的配置文件

使用url统一资源定位符,前提是需要有访问文件的权限。


importjava.io.File;
importjava.net.URL;

importorg.apache.commons.io.FileUtils;

publicclassDownloadURLFile{

/**
*@paramargs
*/
publicstaticvoidmain(String[]args){

Stringres=downloadFromUrl("http://images.17173.com/2010/www/roll/201003/0301sohu01.jpg","d:/");
System.out.println(res);
}


(Stringurl,Stringdir){

try{
URLhttpurl=newURL(url);
StringfileName=getFileNameFromUrl(url);
System.out.println(fileName);
Filef=newFile(dir+fileName);
FileUtils.URLToFile(httpurl,f);
}catch(Exceptione){
e.printStackTrace();
return"Fault!";
}
return"Successful!";
}

(Stringurl){
Stringname=newLong(System.currentTimeMillis()).toString()+".X";
intindex=url.lastIndexOf("/");
if(index>0){
name=url.substring(index+1);
if(name.trim().length()>0){
returnname;
}
}
returnname;
}
}

F. JAVA 读写配置文件时

这个跟工具有关,你用的事eclipse吧,它对后缀为.properties的文件自动的转码中文,变成\啥啥啥的,但是注释我的可以直接显示中午啊,而且对于那些看不到的\xxx鼠标移动上去等一会也可以看到中文

G. java 怎么读取配置文件代码

方式一:采用ServletContext读取,读取配置文件的realpath,然后通过文件流读取出来。
因为是用ServletContext读取文件路径,所以配置文件可以放入在web-info的classes目录中,也可以在应用层级及web-info的目录中。文件存放位置具体在eclipse工程中的表现是:可以放在src下面,也可放在web-info及webroot下面等。因为是读取出路径后,用文件流进行读取的,所以可以读取任意的配置文件包括xml和properties。缺点:不能在servlet外面应用读取配置信息。
具体举例如下:
//ServletContext.getRealPath(name)读取路径
privatevoid test1(HttpServletRequest request, HttpServletResponseresponse)
throwsServletException,IOException {
//response.setContentType("text/html;charset=utf-8");
String path = "/WEB-INF/jdbc_connection.properties"; //读取WEB-INF中的配置文件
String realPath = getServletContext().getRealPath(path);//getServlet

H. 如何在java类中读取Properties配置文件

Filefile=newFile("properties文件路径");
if(file.exists()){
InputStreaminputstream=newFileInputStream(file);
Propertiesproperties=newProperties();
properties.load(inputstream);
properties.getProperty("key");
}

这样就能取到了

I. JAVa读取配置文件位置ReaderProperties类要怎么写

package com;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

/**
* <p>读取配置文件properties文件</p>
*/
public class Configuration {
private Properties pro;
private FileInputStream inputFile;
private FileOutputStream outputFile;

public Configuration(){
pro = new Properties();
}

/**
* <p>初始化Configuration类</p>
* @param filePath 要读取的配置文件的路径+名称
*/
public Configuration(String filePath){
pro = new Properties();
try {
//读取属性文件
inputFile = new FileInputStream(filePath);
//装载文件
pro.load(inputFile);
inputFile.close();
} catch (FileNotFoundException e) {
System.out.println("读取属性文件--->失败!- 原因:文件路径错误或者文件不存在");
e.printStackTrace();
} catch (IOException e) {
System.out.println("装载文件--->失败!");
e.printStackTrace();
}
}

/**
* <p>得到key值</p>
* @param key 取得其值的键
* @return 取得键值
*/
public String getValue(String key){
if(pro.containsKey(key)){
String value = pro.getProperty(key);
return value;
}else{
return "";
}
}

/**
* <p>得到key值</p>
* @param filePath properties文件的路径+文件名
* @param key 取得其值的键
* @return 取得键值
*/
public String getValue(String filePath, String key){
try {
String value = "";
inputFile = new FileInputStream(filePath);
pro.load(inputFile);
inputFile.close();
if(pro.contains(key)){
value = pro.getProperty(key);
return value;
}else{
return "";
}
} catch (FileNotFoundException e) {
e.printStackTrace();
return "";
} catch (IOException e) {
e.printStackTrace();
return "";
} catch (Exception e) {
e.printStackTrace();
return "";
}
}

/**
* <p>清除properties文件中所有的key和其值</p>
*/
public void clear(){
pro.clear();
}

/**
* <p>改变或添加一个key的值</p>
* 当key存在于properties文件中时该key的值被value所代替,
* 当key不存在时,该key的值是value
* @param key 要存入的键
* @param value 要存入的值
*/
public void setValue(String key, String value){
pro.setProperty(key, value);
}

/**
* <p>将更改后的文件数据存入指定的文件中,该文件可以事先不存在</p>
* @param fileName 文件路径+文件名称
* @param description 对该文件的描述
*/
public void saveFile(String fileName, String description){
try {
outputFile = new FileOutputStream(fileName);
pro.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException ioe){
ioe.printStackTrace();
}
}

/**
* 测试方法
* @param args
*/
public static void main(String[] args) {
Configuration conf = new Configuration(".\\.\\WebContent\\WEB-INF\\jdbc.properties");
String value = conf.getValue("hibernate.dialect");
System.out.println(value);
}
}

J. 如何读写ini配置文件 java

//读取一般的属性文件FileInputStreamfin=newFileInputStream("my.ini");//打开文件Propertiesprops=newProperties();//建立属性类props.load(fin);//读入文件fin.close();