Hi all,
Got a problem that has been bugging me for days....
i have a text file which looks like this:
[Database 2]
connection=jdbc:mysql://localhost:3306/gene
driver=com.mysql.jdbc.Driver
username=admin
password=
What i'm trying to do is to change the values of the variables. For eg,
connection=jdbc:mysql://localhost:3306/gene
to
connection=jdbc:mysql://172.20.136.50:3306/gene
This is my code:
import java.io.*;
public class GetConfig
{
public static
String fileName;
public static String secName;
public static String keyName;
public static String changedName;
public static String defStr; //for GetPrivateProfileString
public static RandomAccessFile raf;
public static RandomAccessFile tFile;
public static FileWriter fw;
public static String file;
public static String defString;
public GetConfig()
{
//System.out.println("Config
Test 1");
file = new String("c:\\config.ini");
//System.out.println("Config Test 2");
defString = new String("default");
}
public static void writeString(String rStr, String kStr, String wStr)
{
fileName = file;
secName = rStr;
keyName = kStr;
changedName = wStr;
defStr = defString;
String str;
String temp="";
int no;
long a,b;
try
{
raf = new RandomAccessFile(fileName,"rw");
str = raf.readLine();
A: while(str != null)
{
str = str.trim();
if((str.charAt(0) == '[') && (str.charAt(str.length()-1) == ']'))
{
if(str.substring(1,str.length()-1).equalsIgnoreCase(secName))
{
while((str = raf.readLine())!= null)
{
//System.out.println("hahahahahahahahahahaha");
if((str.trim().charAt(0)) != '[')
{
//a = raf.getFilePointer();
//System.out.println(a +" lalalalaala");
str = str.trim();
no=str.indexOf('=');
temp=str.substring(0,no);
if(temp.equalsIgnoreCase(keyName))
{
a = raf.getFilePointer();
//System.out.println(a +" before");
temp = str.substring(no+1);
raf.seek(a);
raf.writeBytes(changedName);
//b = raf.getFilePointer();
//System.out.println(b +" after");
//System.out.println("Value found " + str + " " +temp);
break A;
}
}
} // while
}
}
str = raf.readLine();
} //while str!=null
raf.close();
}
catch(IOException e)
{
}
}
However, i can't seem to get the file pointer to where i want(after the "=") and start appending the new data.
Can u guys giv me any advice on this?