Save Properties. Read Properties. To File. To XML

//save properties to file
	public static void main(String[] args) {
		Properties properties = new Properties();
		properties.setProperty("a", "Apple");
		properties.setProperty("b", "Bitch");
		properties.setProperty("c", "CC");// set content

		try {
			// Save properties to file. "Info" is the file comment content
			properties.store(new FileOutputStream(new File("d:") + File.separator + "1.properties"), "Info");
		} catch (FileNotFoundException e) {
			e.printStackTrace ();
		} catch (IOException e) {
			e.printStackTrace ();
		}
	}



//read properties file
	public static void main(String[] args) {
		Properties properties = new Properties();
		try {
			//read properties file
			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"));
	}



//Save the attribute in XML
	public static void main(String[] args) {
		Properties properties = new Properties();
		properties.setProperty("q", "Queen");
		properties.setProperty("z", "Zen");
		properties.setProperty("t", "Terry");//Set the content
		
		try {
			//save attribute to xml
			//"Info", comment
			properties.storeToXML(new FileOutputStream(new File("d:"+File.separator+"1.xml")), "Info");
		} catch (FileNotFoundException e) {
			e.printStackTrace ();
		} catch (IOException e) {
			e.printStackTrace ();
		}
	}


/ / Read the attributes of the xml file
	public static void main(String[] args) {
		Properties properties = new Properties();
		try {
			//read
			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"));
	}

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326974609&siteId=291194637