首先,在工程目录下创建配置文件,如下图:
player.properties就是这次创建的配置文件,可以在myeclipse直接打开配置文件并且在图形界面下配置信息。
当然,通过程序的方法读取和写入配置文件的方法如下:
import java.util.Properties; import java.io.*; import org.apache.log4j.*; public class vincent_player_framt { private static Logger logger = Logger.getLogger(vincent_player_framt.class); public static void main(String[] args) throws Exception { // 读取配置文件信息 BufferedInputStream in = new BufferedInputStream( new FileInputStream("player.properties")); Properties pro = new Properties(); pro.load(in); String value = pro.getProperty("hello"); System.out.println(value); in.close(); // 写入配置文件信息 FileOutputStream out = new FileOutputStream("player.properties"); pro.setProperty("hello", "你好"); pro.store(out, "palyer配置文件"); out.close(); } }
pro.store(out, "Comment")中第二个参数是注释信息,会在配置文件的第一行中显示。