overwrite a value inside a file in java -
i create matrix of integers inside file using this,and want overwrite value after creating it,this 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); (int = 0; < 5; i++) { (int j = 0; j < 5; j++) { writer.write(string.valueof(0) + " "); } writer.write("\n"); } } catch (exception e) { e.printstacktrace(); } try { (int = 0; < 5; i++) { (int j = 0; j < 5; j++) { if (i == 3) writer.write(string.valueof(1) + " "); } } writer.close(); } catch (exception e) { e.printstacktrace(); } } }
any suggestions? thanks.
sadly there no way replace 1 value in normal file situation. need form of reading in writing on file.
a best practice in java in scenario store contents in data structure. should use type of matrix in case, array of arrays
int[][] values = new int[x][y];
once created reading file in, need write file same code.
if memory becomes issue, might need stream reading , writing use of temporary file.
i hope helps!
Comments
Post a Comment