问个菜鸟级的问题
.properties文件的作用是不是初始化参数啊?
能不能描述一下它的基本功能和用法吗?

解决方案 »

  1.   

    这个类是用来获取系统属性的
    Properties p=System.getProperties( ); 
    p.list(System.out); 
    你执行看一下就知道这里有啥东东了。
      

  2.   

    .properties文件通常是指以key=value方式存放配置参数的文件。
      

  3.   


    import java.util.Properties;
    import java.io.FileOutputStream;
    import java.io.*;public class TestProperty {  static public void main(String[] args) {    Properties prop = new Properties();    prop.setProperty("head", "头部文件");
        prop.setProperty("title", "头部文件");
        prop.setProperty("form", "表单文件");
        prop.setProperty("body", "身体文件");
        prop.setProperty("table", "表个文件");
        prop.setProperty("tr", "行文件");
        prop.setProperty("td", "单元文件");    FileOutputStream fos1 = null;
        FileOutputStream fos2 = null;    try {
          fos1 = new FileOutputStream("aaa.properties");
          fos2 = new FileOutputStream("bbb.xml");      prop.store(fos1, null);
          prop.storeToXML(fos2, "", "UTF-8");
        }
        catch (IOException ioe) {
          ioe.printStackTrace();
        }
        finally {      try {
            if (fos1 != null) {
              fos1.close();
            }
            if (fos2 != null) {
              fos2.close();
            }
          }
          catch (IOException ioe) {
            ioe.printStackTrace();
          }
        }
      }
    }
      

  4.   

    这个是读取保存操作:import java.util.Arrays;
    import java.io.IOException;
    import java.util.Properties;
    import java.util.Enumeration;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.FileNotFoundException;public class PropertyFileSort {  public static void sortPropertyFile(String fileNameAndPath) {    FileInputStream fis = null;
        FileOutputStream fos = null;
        Properties properties = new Properties();    try {
          fis = new FileInputStream(fileNameAndPath);      if (fis != null) {
            properties.load(fis);
            properties = sortPropertyName(properties);
          }
          fos = new FileOutputStream(fileNameAndPath);
          properties.store(fos, null);
        }
        catch (FileNotFoundException fnfe) {
          fnfe.printStackTrace();
        }
        catch (IOException ioe) {
          ioe.printStackTrace();
        }
        finally {      try {
            if (fis != null) {
              fis.close();
            }
            if (fos != null) {
              fos.close();
            }
          }
          catch (IOException ioe) {
            ioe.printStackTrace();
          }
        }
      }  private static Properties sortPropertyName(Properties properties) {    String[] name = null;
        StringBuffer nameUnite = null;
        Properties prop = new Properties();
        Enumeration enum = properties.keys();    while (enum.hasMoreElements()) {      Object key = enum.nextElement();      String extensePropertyName = (String) key;
          Object extensePropertyValue = properties.get(key);      name = extensePropertyName.split(",");
          Arrays.sort(name);      nameUnite = new StringBuffer();      for (int i = 0; i < name.length; i++) {        if (i == name.length - 1) {
              nameUnite.append(name[i]);
            }
            else {
              nameUnite.append(name[i]).append(",");
            }
          }      prop.put(nameUnite.toString(), extensePropertyValue);
        }    return prop;
      }  public static void main(String[] args) {    sortPropertyFile("d:\\a.properties");
      }
    }注:enum在jdk1.5中已成关键字,如果出现变异错误请把枚举对象改名。
      

  5.   

    读写用ResourceBundle蛮不错的。