保存属性.读取属性.到文件.到XML

//保存属性,到文件
	public static void main(String[] args) {
		Properties properties = new Properties();
		properties.setProperty("a", "Apple");
		properties.setProperty("b", "Bitch");
		properties.setProperty("c", "CC");// 设置内容

		try {
			// 保存属性到文件。“Info”是文件注释内容
			properties.store(new FileOutputStream(new File("d:") + File.separator + "1.properties"), "Info");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}



//读取属性文件
	public static void main(String[] args) {
		Properties properties = new Properties();
		try {
			//读取属性文件
			properties.load(new FileInputStream(new File("d:"+File.separator+"1.properties")));
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(properties.getProperty("a"));
		System.out.println(properties.getProperty("b"));
	}



//将属性保存在XMl
	public static void main(String[] args) {
		Properties properties = new Properties();
		properties.setProperty("q", "Queen");
		properties.setProperty("z", "Zen");
		properties.setProperty("t", "Terry");//设置内容
		
		try {
			//保存属性到xml
			//"Info",注释
			properties.storeToXML(new FileOutputStream(new File("d:"+File.separator+"1.xml")), "Info");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


//读取xml文件的属性
	public static void main(String[] args) {
		Properties properties = new Properties();
		try {
			//读取
			properties.loadFromXML(new FileInputStream(new File("d:"+File.separator+"1.xml")));
		} catch (InvalidPropertiesFormatException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(properties.getProperty("t"));
	}

你可能感兴趣的:(保存属性.读取属性.到文件.到XML)