请问各位如何用Properties类来读取properties文件。而且要将他输入读取出来的放进文本框中?

解决方案 »

  1.   


    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"));
    }
    }
      

  2.   

    读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);
    }
    }}
      

  3.   

    我是这样读的Properties pp=new Properties();
    pp.load(new FileInputStream("D:\aa.properties"));String name=pp.getProperty("name");
    String age=pp.getProperty("age");自己改一下
      

  4.   

    这要在JSP中进行的,当我打开JSP的时候,那么数据就已经取出来了。
      

  5.   


    JSP编译后也是一个class文件。(或者你把你的JSP当成一个java 的类来用,那也是可以的。)只要你明白这点,至于怎么用,楼上的各位已经说得很清楚了。
      

  6.   


    <!--引入包>
    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%>">