java, outputting array to file - not working as expected

GeX

GeX

Soldato
Joined
17 Dec 2002
Posts
6,982
Location
Manchester
Hi. Wonder if anyone can help me here.

Basically what i'm working on is a bit of code that reads an array from a file, sorts it, and then writes it back. It's not part of any bigger project, i'm just trying to understand how it all works :)

Reading from the file works fine, sorting the array works fine, but writing back to the file does not. It only outputs one element of the array, despite outputting the sorted array to the screen correctly. It is probably a silly mistake that i've made and can't spot it for looking.

Code:
	for (int i = 0; i < test.length; i++)
	     {




	       String[] tempRow = test[i];
	       for (int j = 0; j < tempRow.length; j++)
	       {

	        System.out.print(tempRow[j] + " | "); //debug screen output

	    try {

	    BufferedWriter out = new BufferedWriter(new FileWriter("c://java//output.txt"));
		out.write(tempRow[j] + " | ");
        out.close();
            } catch (IOException e) { }

	       }
	       System.out.println(); // debug screen output
		}

What I don't get is, despite the code handling.. actually.. i think i just understood... It is re-writing the file everytime isn't it. Balls. There is only one element appearing because that is the last one to be written.. hmmm... help?
 
hmm blonde moment, nevermind - sorted now. amazing what a cup of coffee does :D
 
Back
Top Bottom