1

I have a program that loads lines from a user file, then selects the last part of the String (which would be an int)

Here's the style it's saved in:

nameOfValue = 0
nameOfValue2 = 0

and so on. I have selected the value for sure - I debugged it by printing. I just can't seem to save it back in.

if(nameOfValue.equals(type)) {
        System.out.println(nameOfValue+" equals "+type);
            value.replace(value, Integer.toString(Integer.parseInt(value)+1));
        }

How would I resave it? I've tried bufferedwriter but it just erases everything in the file.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
user1032232
  • 47
  • 1
  • 2
  • 5
  • 2
    Any reason you're not just using `Properties`? In any case, unless you do some irritating random access stuff, you need to write all the properties back out. See stuff like [this](http://stackoverflow.com/questions/3147615/replace-string-in-file). – Dave Newton Nov 07 '11 at 02:42

3 Answers3

5

My suggestion is, save all the contents of the original file (either in memory or in a temporary file; I'll do it in memory) and then write it again, including the modifications. I believe this would work:

public static void replaceSelected(File file, String type) throws IOException {

    // we need to store all the lines
    List<String> lines = new ArrayList<String>();

    // first, read the file and store the changes
    BufferedReader in = new BufferedReader(new FileReader(file));
    String line = in.readLine();
    while (line != null) {
        if (line.startsWith(type)) {
            String sValue = line.substring(line.indexOf('=')+1).trim();
            int nValue = Integer.parseInt(sValue);
            line = type + " = " + (nValue+1);
        }
        lines.add(line);
        line = in.readLine();
    }
    in.close();

    // now, write the file again with the changes
    PrintWriter out = new PrintWriter(file);
    for (String l : lines)
        out.println(l);
    out.close();

}

And you'd call the method like this, providing the File you want to modify and the name of the value you want to select:

replaceSelected(new File("test.txt"), "nameOfValue2");
Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Thanks, this helped alot. I was thinking about loading into a seperate file, then deleting the original, but using memory would be a much better idea in my case. – user1032232 Nov 08 '11 at 00:23
1

I think most convenient way is:

  1. Read text file line by line using BufferedReader
  2. For each line find the int part using regular expression and replace it with your new value.
  3. Create a new file with the newly created text lines.
  4. Delete source file and rename your new created file.

Please let me know if you need the Java program implemented above algorithm.

Upul Bandara
  • 5,973
  • 4
  • 37
  • 60
0

Hard to answer without the complete code...

Is value a string ? If so the replace will create a new string but you are not saving this string anywhere. Remember Strings in Java are immutable.

You say you use a BufferedWriter, did you flush and close it ? This is often a cause of values mysteriously disappearing when they should be there. This exactly why Java has a finally keyword.

Also difficult to answer without more details on your problem, what exactly are you trying to acheive ? There may be simpler ways to do this that are already there.

Newtopian
  • 7,543
  • 4
  • 48
  • 71