update midle values of the text file...

hello all

I can write to a NEW text file "myfile.txt" by using:

BufferedWriter out = new BufferedWriter(new FileWriter("myfile.txt"));

out.write(databaseName +"\n");

out.write(hostName +"\n");

out.write(portName +"\n");

out.write(userName +"\n");

out.write(pass +"\n");

...

out.write(value1 +"\n");

out.write(value2 +"\n");

...

Now supose that the file "myfile.txt" is already exited and I only want to write to midle of the file (e.g I want to update the value1-line 6 and value2 - line 7 and want to keep other values in the file)

How I can do that...

Many thanks

shuhu

[668 byte] By [suhua] at [2007-11-27 6:11:17]
# 1
One way is to use RandomAccessFile, you can also read what you need from the existing and build a new file adding the new values. http://www.exampledepot.com/egs/java.io/UseRandomAccessFile.htmlMessage was edited by: _helloWorld_
_helloWorld_a at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...
# 2

You can't write to a file that isn't open, and you can't insert text into the middle of the file without writing over anything... what you have to do is open the file, read to where you want to write, then write your line, and then write out the rest of the file.

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.FileReader;

import java.io.FileWriter;

import java.util.ArrayList;

public class ReplaceMe {

public static int PERSISTENT = 1;

public static void main(String[] args) {

System.out.println("PERSISTENT = " + PERSISTENT);

try {

BufferedReader br = new BufferedReader(

new FileReader("ReplaceMe.java") );

ArrayList<String> tehCodez = new ArrayList<String>();

String line;

while ( (line = br.readLine()) != null) {

if ( line.contains("int PERSISTENT = ") &&

line.contains("static") ) {

tehCodez.add("public static " +

"int PERSISTENT = " + (PERSISTENT+1) + "; // Found it.");

} else {

tehCodez.add(line);

}

}

br.close();

BufferedWriter bw = new BufferedWriter(

new FileWriter("ReplaceMe.java") );

for (String aLine: tehCodez) {

bw.write(aLine);

bw.newLine();

}

bw.close();

Runtime.getRuntime().exec("javac ReplaceMe.java");

} catch(Exception e) {

e.printStackTrace();

}

}

}

kevjavaa at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...
# 3
> One way is to use RandomAccessFileA dicey proposition with a text file, and doesn't look appropriate for this example.Message was edited by: Hippolyte
Hippolytea at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...
# 4
> out.write(databaseName +"\n");By the way, that's a poor way to start a new line -- platform independence and all that. BufferedWriter has a specific method to do that: newLine().
Hippolytea at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...
# 5
> > One way is to use RandomAccessFile> > A dicey proposition with a text file, and doesn't> look appropriate for this example.> Fair point. But, can I use Hungarian notation on my variables?
_helloWorld_a at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...
# 6
> But, can I use Hungarian notation on my variables?pYou vcan vdo pthat cif pyou vlike.
DrClapa at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...
# 7
> > But, can I use Hungarian notation on my variables?> > pYou vcan vdo pthat cif pyou vlike.char* m_lpszYep = L"Yer darn tootin!";
kevjavaa at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...
# 8

Thanks all

In my solution, i have to use three separated files: one for some first data, one for middle data and one for ....some last data....

So I donot have to rewrite whole data if there is only one file.

And this is not good solution

Message was edited by:

suhu

suhua at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...
# 9
Given the data you posted, I'd suggest moving from a fixed-line-position notation to something richer, like java.util.Properties files, or XML, or an actual relational database. Properties files are probably easiest.
paulcwa at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...
# 10
ThanksBut I think that the properties file cannot be modified. It can be read only ?regards
suhua at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...
# 11
> But I think that the properties file cannot be modified. It can be read only ?False. A file is a file, unless you are going to be playing with file persmissions?
Hippolytea at 2007-7-12 17:17:29 > top of Java-index,Java Essentials,New To Java...