wood burning stoves*
The moose likes I/O and Streams and the fly likes Changing file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Changing file" Watch "Changing file" New topic
Author

Changing file

Jon Ding
Greenhorn

Joined: Mar 23, 2004
Posts: 4
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?
Eddie Vanda
Ranch Hand

Joined: Mar 18, 2003
Posts: 281
Before you try to append your bytes use:
raf.seek (raf.length ());
To get to the end of the file.


The nice thing about Standards is that there are so many to choose from!
Jon Ding
Greenhorn

Joined: Mar 23, 2004
Posts: 4
Thanks eddie...
However, i think u hav misunderstood what i meant...
Using raf.seek (raf.length ());
will get me to the end of the file but that's nt what i want...
What i want is to change the data after the "=" sign...
Cos there are many records in my file... e.g,
[Database 1]
connection=jdbc:mysql://123.58.56.99:3306/disorder
driver=com.mysql.jdbc.Driver
username=jon
password=hihi
[Database 2]
connection=jdbc:mysql://localhost:3306/gene
driver=com.mysql.jdbc.Driver
username=admin
password=
[Database 3]
connection=jdbc:mysql://172.20.135.66:3306/disease
driver=com.mysql.jdbc.Driver
username=ding
password=lala
so let's say i want to change the username for database 2, i would call on the writeString function, e.g,
writeString(database 2,username, "[new username here]");
And if the new username is "eddie", after calling the function my file should look like this:
[Database 1]
connection=jdbc:mysql://123.58.56.99:3306/disorder
driver=com.mysql.jdbc.Driver
username=jon
password=hihi
[Database 2]
connection=jdbc:mysql://localhost:3306/gene
driver=com.mysql.jdbc.Driver
username=eddie
password=
[Database 3]
connection=jdbc:mysql://172.20.135.66:3306/disease
driver=com.mysql.jdbc.Driver
username=ding
password=lala
I'm sorry if my explaination's not clear, however i will try to improve

Pls feel free to ask me anything u dun understand...
Thanks a lot again!
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

I guess you have to close the connection and reopen it with the new username/ passwd.


http://home.arcor.de/hirnstrom/bewerbung
Jon Ding
Greenhorn

Joined: Mar 23, 2004
Posts: 4
Originally posted by Stefan Wagner:
I guess you have to close the connection and reopen it with the new username/ passwd.

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
You can only update fields like that when they are exactly the same length. You can't insert or delete any characters in your file, only overlay them. I'd read the file with readln(),change lines as I find them and write a new file. Here's a scheme I've mentioned before:
read original
write new temp
close original and temp
rename original to backup
rename temp to original
erase backup
You can read & write at the same time - read one, write one - or read the whole file into memory, modify it in memory, write it all out to disk. Either way works.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Jon Ding
Greenhorn

Joined: Mar 23, 2004
Posts: 4
Originally posted by Stan James:
You can only update fields like that when they are exactly the same length. You can't insert or delete any characters in your file, only overlay them. I'd read the file with readln(),change lines as I find them and write a new file. Here's a scheme I've mentioned before:
read original
write new temp
close original and temp
rename original to backup
rename temp to original
erase backup
You can read & write at the same time - read one, write one - or read the whole file into memory, modify it in memory, write it all out to disk. Either way works.

Well i guess tat's the only way it'll work... Was hoping i could juz change the chracters...
Thanks a lot guys!
 
jQuery in Action, 2nd edition
 
subject: Changing file