• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Changing file

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Before you try to append your bytes use:
raf.seek (raf.length ());
To get to the end of the file.
 
Jon Ding
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you have to close the connection and reopen it with the new username/ passwd.
 
Jon Ding
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Stefan Wagner:
I guess you have to close the connection and reopen it with the new username/ passwd.


 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Jon Ding
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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!
 
Don't touch me. And dont' touch this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic