Newsgroup: it.comp.java
Titolo: File "INI" in Java.

Salve, volevo chiedervi se esiste una classe Java per la gestione
dei file di configurazione (tipo file INI di Windows).
Grazie in anticipo.
Ciao.
Marco.

 ha scritto nel messaggio
news:fzoU6.6613$a44....@news1.tin.it...


Io ho qualcosa di meglio. Ciao.
Romano
import java.io.*;
import java.util.*;
public class IniFile {
  public Vector vIniContents = new Vector();
  public String sIniFileName;
  public IniFile(String sFileName) {
    sIniFileName = sFileName;
    getFile();
  }
  public String getValue(String sSection, String sParameter) {
    // find section
    // find parameter
    // return value
    String sFindSection = "[" + sSection.toUpperCase() + "]";
    int nSection = findSection(sFindSection);
    if (nSection == -1) {
      return ("");
    }
    int nParameter = findParameter(nSection, sParameter.toUpperCase());
    if (nParameter == -1) {
      return ("");
    }
    StringTokenizer st =
      new StringTokenizer((String)vIniContents.elementAt(nParameter),"=");
    st.nextToken(); // left side
    return (st.nextToken()); //right side
  }
  public void setValue(String sSection, String sParameter, String sValue) {
    // find section
    //  found? find parameter
    //   found parameter
    //    delete
    //    add parameter value
    //   not found?
    //
    //    add parameter/value
    //   not found, add section
    //   jump to find value
    //
    int nSection, nParameter;
    String sFindSection = "[" + sSection.toUpperCase() + "]";
    String sFindParameter = sParameter.toUpperCase();
    nSection = findSection(sFindSection);
    if (nSection == -1) { //Section not found
      vIniContents.addElement(sFindSection);
      nSection = vIniContents.size()-1;
    }
    // now search for parameter
    nParameter = findParameter(nSection, sFindParameter);
    // if the parameter wasn't found, insert at nSection + 1
    // if the parameter was found, it is removed
    if (nParameter == -1) {
      nParameter = nSection + 1;
    } else {
      vIniContents.removeElementAt(nParameter);
    }
    vIniContents.insertElementAt(sParameter.toUpperCase() + "=" +
      sValue.toUpperCase(), nParameter);
    putFile();
  }
  public void getFile() {
    String sLineBuffer;
    RandomAccessFile fdIni = null;
    try {
      fdIni = new RandomAccessFile(sIniFileName,"r");
      while (((sLineBuffer = fdIni.readLine()) != null)) {
        if (sLineBuffer.endsWith("\r")) {
          sLineBuffer = new
String(sLineBuffer.substring(0,sLineBuffer.length()-1));
        }
        vIniContents.addElement(sLineBuffer.toUpperCase());
      }
      fdIni.close();
    } catch (IOException e) {
    }
  }
  private int findSection(String sFindSection) {
    int nSection = -1;
    // find section header
    for (int nLoop = 0; nLoop < vIniContents.size(); ++nLoop) {
      if (((String)vIniContents.elementAt(nLoop)).compareTo(sFindSection) ==
0) {
        nSection = nLoop;
        nLoop = vIniContents.size();
      }
    }
    return (nSection);
  }
  private int findParameter(int nSection, String sFindParameter) {
    int nParameter = -1;
    String sParameter = "Not Found";
    for (int nLoop = nSection+1; nLoop < vIniContents.size(); nLoop++) {
      StringTokenizer st =
        new StringTokenizer((String)vIniContents.elementAt(nLoop),"=");
      sParameter = st.nextToken();
      if (sParameter.compareTo(sFindParameter) == 0) {
        nParameter = nLoop;
        nLoop = vIniContents.size();
      } else {
        if (((String)vIniContents.elementAt(nLoop)).startsWith("[") == true)
{
          nLoop = vIniContents.size();
        }
      }
    }
    return (nParameter);
  }
  public void putFile() {
    FileOutputStream out = null;
    try {
      out = new FileOutputStream(sIniFileName);
    } catch (IOException e) {
      e.printStackTrace();
    }
    PrintWriter fdIni = new PrintWriter(out);
    int nLoop;
    String sOutput;
    for (nLoop = 0; nLoop < vIniContents.size(); ++nLoop) {
      sOutput = (String) vIniContents.elementAt(nLoop);
      fdIni.println(sOutput);
    }
    fdIni.close();
  }
}