Java IO Stream (properties)

Source: Internet
Author: User

/*
* 1.Properties is a subclass of Hashtable:
* He has the features of the map collection, and the key-value pairs stored in it are strings.
* is a collection container combined with IO technology.
* Features of this object: can be used for key-value pairs in the form of configuration files, operating on the hard disk key-value pairs.
*
* 2. Store the data in the stream in the collection (the Load method principle):
* Associated with a stream and a info.txt file.
* Reads a row of data and uses "=" to cut the row data.
* The left side of the equals sign is the key, and the right value is stored in the Properties collection.
* When loading data, the data needs to be in a fixed format, key = value.
*/
Import java.util.*;
Import java.io.*;
public class Propertiesdemo {
public static void Main (string[] args) throws IOException {
Getandset ();
Method_1 ();
Loadedemo_1 ();
}
To store data from a stream in a collection
public static void Loadedemo_1 () throws IOException
{
FileReader f=new FileReader ("C:\\demo1.txt");
Properties Pro=new properties ();
Pro.load (f);
Lists the collection directories.
Pro.list (System.out);
SOP (PRO);
Pro.setproperty ("Wangwu", "39");
FileWriter fw=new FileWriter ("C:\\demo1.txt");
Pro.store (FW, "Hahahaha");
Pro.list (System.out);
F.close ();
Fw.close ();
}
public static void Method_1 () throws IOException
{
Properties Pro=new properties ();
BufferedReader buf=new BufferedReader (New FileReader ("C:\\demo1.txt"));
String Line=null;
while ((Line=buf.readline ())!=null)
{
String arr[]=line.split ("=");

Pro.setproperty (arr[0],arr[1]);
}
SOP (PRO);
}
public static void Getandset ()
{
Properties Pro=new properties ();
Pro.setproperty ("Zhangsan", "30");
Pro.setproperty ("Lisi", "25");
SOP (PRO);
SOP (Pro.getproperty ("Lisi"));
Pro.setproperty ("Lisi", 99+ "");
For (String s:pro.stringpropertynames ())
{
SOP (s+ "---" +pro.getproperty (s));
}
}
public static void Sop (Object obj)
{
System.out.println (obj);
}

}


/*
Needs
* Used to record the number of times the application ran, and if the number of uses has been reached, the registration prompt.
* A configuration file is required to save the number of times each use, and the data in the file is in the form of a key value pair.
* Configuration files can be used to share application data.
*/
Import java.io.*;
Import java.util.*;
public class PropertiesDemo2 {
public static void Main (string[] args) throws IOException {
File F=new file ("C://demo2.ini");
if (!f.exists ())
F.createnewfile ();
FileInputStream fi=new FileInputStream (f);
Properties Pro=new properties ();
Pro.load (FI);
String Value=pro.getproperty ("Time");
int count=0;
if (value!=null)
{

Count=integer.parseint (value);
if (count>=5)
SOP ("Trial times are full, please purchase the official version");
}
count++;
Pro.setproperty ("Time", count+ "");
FileOutputStream fos=new FileOutputStream (f);
Pro.store (FOS, "LOL");
Fi.close ();
Fos.close ();
}
public static void Sop (Object obj)
{
System.out.println (obj);
}
}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Java IO Stream (properties)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.