0

i create a matrix of integers inside a file using this,and i want to overwrite a value after creating it,this is the code:

    import java.io.File;
import java.io.FileWriter;

public class main {
    public static void main(String[] args) {
    File file = new File("foo/file.txt");
    FileWriter writer = null;
    try {
        file.createNewFile();
        writer = new FileWriter(file);
        for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {

            writer.write(String.valueOf(0) + " ");

        }
        writer.write("\n");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {
            if (i == 3)
            writer.write(String.valueOf(1) + " ");

        }
        }
        writer.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    }
}

any suggestions? thanks.

OiRc
  • 1,602
  • 4
  • 21
  • 60
  • What do you mean by "doesn't work". What happens? And can you please show all of the code of the "not working" example, in case the error is in the "..." part? – Dawood ibn Kareem May 29 '14 at 21:29
  • "any suggestion?" Yes. Don't do that. Write the modified contents to a _new file_ and only if successful rename to the original file. What do you think will happen if overwriting fails? – fge May 29 '14 at 21:30
  • by `"doesn't work"` i mean that the matrix remain the same,no changes are apported, after calling `overwrite()` – OiRc May 29 '14 at 21:30
  • For one, the body of the `if` statement will execute more than once... i.e., for the entire row (`i` will be 4 for each value of `j`). If you want to just write a single value, you'll have to narrow it down more. – Chris Dargis May 29 '14 at 21:31
  • I'm quite sure you don't mean "apported" - Java isn't quite that magical. Is this the same `writer` you had earlier, so that the new text will be appended to the previous output? Or are you trying to write a new file? Or overwriting the same file you created before? Please clarify what you're doing. – Dawood ibn Kareem May 29 '14 at 21:44
  • @DavidWallace see update into question. – OiRc May 29 '14 at 21:46
  • yes,it is the same writer,after overwrite i close it,i don't create a new writer. – OiRc May 29 '14 at 21:48
  • OK, so with the latest version of the code above, aren't you getting a line of 1s at the bottom of the file, after all the 0s? – Dawood ibn Kareem May 29 '14 at 21:48
  • @DavidWallace i'm not getting a line of 1,this it the point!, i don't understand why... – OiRc May 29 '14 at 21:50
  • OK, I ran your program, exactly as you've written it above, and my output is `0 0 0 0 0 //0 0 0 0 0 //0 0 0 0 0 //0 0 0 0 0 //0 0 0 0 0 //1 1 1 1 1` where I've used `//` to denote a newline. Can you please state in your question what you're getting instead of that? – Dawood ibn Kareem May 29 '14 at 21:59
  • @DavidWallace you're right, i saw the result of a different file, we got the same result,but the problem remains, why the last row is overwritten instead the desired row? – OiRc May 29 '14 at 22:02
  • So you're trying to "undo" the data that you've already written? Is that what you're saying? If so, probably the easiest thing to do is to cache the data somewhere, and rewrite the file as needed. – Dawood ibn Kareem May 29 '14 at 22:03
  • you centered the problem, what do you mean by `cache the data`?, matrix? or something like this,is there any fastest way?. – OiRc May 29 '14 at 22:06
  • Well, which data structure is most efficient depends on the type of data. If what you have is a 5x5 matrix of integers, then probably the easiest thing is to use an `int[][]` as described in tmanion's answer. – Dawood ibn Kareem May 29 '14 at 22:10
  • @DavidWallace so the old writer,is useless, or it can be used for the "overwrite"? as tmanion says. – OiRc May 29 '14 at 22:13
  • You should create a new writer each time you want to write to a file. – Dawood ibn Kareem May 29 '14 at 22:36

3 Answers3

1

Sadly there is no way to just replace one value in a normal file for this situation. You will need to do some form of reading in then writing over the file.

A best practice in Java in a scenario like this would be to store the contents in a data structure. You should use some type of matrix in this case, which is just an array of arrays

int[][] values = new int[x][y]; 

Once that is created from reading the file in, you just need to write that to the file with the same code.

If memory becomes an issue, then you might need to look into stream reading and writing with the use of a temporary file.

I hope that helps!

tmanion
  • 401
  • 2
  • 11
0

Is there any specific reason why you need to overwrite one value instead of rewriting entire file? In C++ you can manually set write position in a stream, but it will overwrite anything that follows.

Anyway, check out this question and answers: how to write content in a Specific position in a File

Community
  • 1
  • 1
Zibi
  • 350
  • 1
  • 13
-2

Look into Random Access File. If it does not work, you might have to rewrite the whole file.

  • 1
    Considering that there's a lot of unknowns about how the file is accessed, this doesn't seem like a suitable answer. – Makoto May 29 '14 at 21:33