天天看点

Properties Runtime

package ZHANG.API;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Properties;

public class PropertiesFile {

 public static void main(String[] args)  {

  Properties settings = new Properties();

  try {

   settings.load(new FileInputStream("count.txt"));

  } catch (FileNotFoundException e) {

   settings.setProperty("count", String.valueOf(0));

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

  //settings.get("count");

  int i = Integer.parseInt(settings.getProperty("count"))+1;

  System.out.println("这是第"+i+"次运行..");

  //settings.put("count",new Integer(i).toString());

  settings.setProperty("count", new Integer(i).toString());

  try {

   settings.store(new FileOutputStream("count.txt"), "program is use:");

  } catch (FileNotFoundException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  } catch (IOException e) {

   // TODO Auto-generated catch block

   e.printStackTrace();

  }

 }

}

package ZHANG.API;

import java.io.IOException;

import java.util.Enumeration;

import java.util.Properties;

public class TestProperties {

 public static void main(String[] args) throws InterruptedException {

  Properties sp = System.getProperties();

  Enumeration e = sp.propertyNames();

  while(e.hasMoreElements()){

   String key = (String) e.nextElement();

   System.out.println(key+"="+sp.getProperty(key));

  }

  Process p = null;

  try {

   p = Runtime.getRuntime().exec("notepad.exe TestProperties.java");

   Thread.sleep(5000);

   p.destroy();

  } catch (IOException e1) {

   // TODO Auto-generated catch block

   e1.printStackTrace();

  }

 }

}