INI parameter (text file) reading and writing

Source: Internet
Author: User
Tags ini readline trim

Parameters (text file) read-write, by chillming


import java.io.*;


public class inifile{


private String FileName = "";


public IniFile (String fn) {


FileName = fn;


}


//Returns the value of the parameter


public string Readpara (string para,string PV) {


String pname = "";


String pvalue = PV;


String linestr = "";


int pos = 0;


try{


BufferedReader rfile = new BufferedReader (new FileReader (FileName));


while (true) {


try{


linestr = Rfile.readline ();


pos = linestr.indexof ("=");


pname = linestr.substring (0,pos). Trim ();


if (Pname.equals (Para.trim ()) {


//Find


pvalue = linestr.substring (Pos+1,linestr.indexof (";")). Trim ();


break;


}


}catch (NullPointerException e) {


//File tail


break;


}


}


Rfile.close ();


}catch (IOException e)


{


if (pv==null) PV = "";


return PV;


}


return pvalue;


}


//Update or add Parameters


public boolean Writepara (String pn,string PV) {


String pname = "";


String pvalue = "";


String linestr = "";


String alllines = "";


int pos = 0;


Boolean ishere = false;


if (pv==null) PV = "";


try{


BufferedReader rfile = null;


try{


rfile = new BufferedReader (new FileReader (FileName));


}


catch (FileNotFoundException e) {


//file does not exist


File File = new file (FileName);


File.createnewfile ();


Rfile = new BufferedReader (new FileReader (FileName));


}


while (true) {


try{


linestr = Rfile.readline ();


pos = linestr.indexof ("=");


pname = linestr.substring (0,pos). Trim ();


pvalue = linestr.substring (Pos+1,linestr.indexof (";")). Trim ();


if (pname.equals (PN)) {


linestr = pname + "=" + PV + "; \ n";


Ishere = true;


}else{


linestr = pname + "=" + pvalue + "; \ n";


}


if (linestr!=null && linestr!= "") {


Alllines + = Linestr;


}


}catch (NullPointerException e) {


//File tail


break;


}


}


Rfile.close ();


}catch (IOException e) {


return false;


}


try{


BufferedWriter fout=new BufferedWriter (New FileWriter (FileName));


if (!ishere) {


linestr = PN + "=" + PV + "; \ n";


Alllines + = Linestr;


}


Fout.write (Alllines);


Fout.close ();


}catch (IOException e)


{


System.out.println ("IO error write");


return false;


}


return true;


}


//Delete parameters


public boolean Delpara (String PN) {


String pname = "";


String linestr = "";


String alllines = "";


int pos = 0;


try{


BufferedReader rfile = new BufferedReader (new FileReader (FileName));


while (true) {


try{


linestr = Rfile.readline ();


pos = linestr.indexof ("=");


pname = linestr.substring (0,pos). Trim ();


if (! Pname.equals (PN)) {


Alllines + = linestr + "\ n";


}


}catch (NullPointerException e) {


//File tail


break;


}


}


Rfile.close ();


}catch (IOException e) {


return false;


}


try{


bufferedwriter fout=new BufferedWriter (New FileWriter (FileName));


Fout.write (Alllines);


Fout.close ();


}catch (IOException e)


{


return false;


}


return true;


}


public static void Main (String args[]) {


String fn = "Config.txt";


String Pv = "";


//system.out.println (System.getproperty ("User.dir"));


IniFile IniFile = new IniFile (FN);


Inifile. Writepara ("Java", "chillming");


Inifile. Writepara ("Java1", "chillming1");


Inifile. Writepara ("Java2", "chillming2");


Inifile. Delpara ("JAVA1");


Pv = Inifile. Readpara ("Java2", "java2");


System.out.println (PV);


Pv = Inifile. Readpara ("Java1", "JAVA1");


System.out.println (PV);


Pv = Inifile. Readpara ("Java", "java0");


System.out.println (PV);


}


}

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.