请问各位如何用Properties类来读取properties文件。而且要将他输入读取出来的放进文本框中?
解决方案 »
- JAVA:这段代码有什么问题?
- interface究竟是什么?
- 关于JComboBox的一个奇怪的问题(今天要解决,在线等待)
- 请问这是什么问题?Exception in thread "main" java.lang.NoClassDefFoundError: \java\jframedemo/class
- 请各位GGJJDDMM帮忙
- 如何用命令单步调试,单步跟踪java 程序
- 请教各位:为何我的java 的applet无法初始化?原来可用的(源代码如下)急急急急急急
- 今天我生日,特来散分!^-^
- 我现在刚开始学习java,看的书是《java编程百事通》,我有c++基础,但还不是很清楚怎么学?请各位给点建议!!谢谢了。
- jbuilder和weblogic环境中如何做jar,war,ear文件?
- 再战LUCKY52!!!
- java会不会被淘汰
package com.train.first;import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;public class Example
{
public static void main(String[] args) throws Exception
{
InputStream in = null;
try
{
in = new FileInputStream("test.properties");
}
catch (IOException e)
{
e.printStackTrace();
}
Properties properties = new Properties();
if (null != in)
{
try
{
properties.load(in);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (null != in)
{
try
{
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
}
System.out.println(properties.getProperty("bb"));
}
}
至于放到文本框里,那个不是问题吧..package org.shadowlin.util;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Properties;import org.apache.log4j.Logger;public class PropertiesLoader {
private static Logger logger=Logger.getLogger(PropertiesLoader.class);
private Properties propertie;
private InputStream inputFile;
private OutputStream outputFile; /***/
/**
* Constructor
* @param filePath File path of properties file
*/
public PropertiesLoader(String filePath) {
propertie = new Properties();
try {
inputFile = ClassLoader.getSystemResourceAsStream(filePath);
propertie.load(inputFile);
inputFile.close();
} catch (FileNotFoundException ex) {
logger.warn("Can not find properties file,wrong path or file doesn't exist");
logger.warn(ex);
} catch (IOException ex) {
logger.warn("Load properties file failed");
logger.warn(ex);
} catch(Exception e){
logger.warn("Load properties file failed");
logger.warn(e);
}
}// end ReadConfigInfo() /***/
/**
* Get value by key
* @param key
*
* @return the value of key
*/
public String getValue(String key) {
if (propertie.containsKey(key)) {
String value = propertie.getProperty(key);
if ("".equals(value)) {
value = null;
}
return value;
} else
logger.warn("can't find key");//
return null;
}
/*
* public Iterator<String> getAllValues(){ try{ Iterator<String> values
* =(Iterator<String>)propertie.elements(); return values; }catch(Exception
* ex){ ex.printStackTrace(); return null; } }
*
*
* public Iterator<String> getAllKeys(){ try{ Iterator<String>
* keys=(Iterator)propertie.keys(); return keys; } catch (Exception ex) {
* ex.printStackTrace(); return null; } }
*/ /***/
/**
* Clear all keys and values of properties file
*/
public void clear() {
propertie.clear();
} /***/
/**
* Change the value of this key,when key dosen't exist create a key with this value
*
* @param key
* @param value
*
*/
public void setValue(String key, String value) {
propertie.setProperty(key, value);
} /***/
/**
* Save the changed properties file
*
* @param fileName
*
* @param description
*
*/
public void saveFile(String fileName, String description) {
try {
outputFile = new FileOutputStream(fileName);
propertie.store(outputFile, description);
outputFile.close();
} catch (FileNotFoundException e) {
logger.warn(e);
} catch (IOException ioe) {
logger.warn(ioe);
}
}}
pp.load(new FileInputStream("D:\aa.properties"));String name=pp.getProperty("name");
String age=pp.getProperty("age");自己改一下
JSP编译后也是一个class文件。(或者你把你的JSP当成一个java 的类来用,那也是可以的。)只要你明白这点,至于怎么用,楼上的各位已经说得很清楚了。
<!--引入包>
import java.util.Properties;
import java.io.FileInputStream;<!--取值-->
<%
Properties pp=new Properties();
pp.load(new FileInputStream("D:\aa.properties"));String name=pp.getProperty("name");
String age=pp.getProperty("age");
%>
<!--赋值-->
<input type="text" value="<%=name%>">
<input type="text" value="<%=age%>">