请问各位如何用Properties类来读取properties文件。而且要将他输入读取出来的放进文本框中? 请问各位如何用Properties类来读取properties文件。而且要将他输入读取出来的放进文本框中? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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")); }} 读properties的类有好多现成的,给你个我在用的.至于放到文本框里,那个不是问题吧..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); } }} 我是这样读的Properties pp=new Properties();pp.load(new FileInputStream("D:\aa.properties"));String name=pp.getProperty("name");String age=pp.getProperty("age");自己改一下 这要在JSP中进行的,当我打开JSP的时候,那么数据就已经取出来了。 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%>"> 关于统计字符数组中同一字符出现的次数 关于Java设置鼠标光标的问题,大家来讨论一下。 请问请问 初学jsp的编辑器问题 怎样禁止JToolbar被用户拖动? java图形编程盼高手帮忙 线程问题,请高手指教 在论坛上发言无意中泄露公司秘密被老板批评者,请进! 结构的用法? 新人求助,javaSE-12环境,在eclipse中新建项目后new一个class报错 再战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%>">