Bagus W Bagus W - 11 months ago 71
Java Question

Saving Sequential data in Java

I have some problems about saving sequential data (Contour Area), I have a project that detect contour. And I want to save data contour area in my android. I already save that contour area in file, but its just the last one, not a sequential data(all contour area) .
here's my contour code:

Imgproc.findContours(mDilated, contours, hierarchy, Imgproc.RETR_TREE, Imgproc.CHAIN_APPROX_SIMPLE);
int a =15;
float b = (float)4.55;
double maxVal = 0;
int maxValIdx = 0;
for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ ) {
double contourArea = Imgproc.contourArea(contours.get(contourIdx));
if (maxVal < contourArea) {
maxVal = contourArea;
maxValIdx = contourIdx;

Here's my code for saving file:

String taString = Integer.toString(a)+","+Float.toString(b)+","+Double.toString(maxVal);

and this the writeToFile function :

private void writeToFile(String data) {
try {
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/MyFiles");

File file = new File(directory, "mysdfile.txt");
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
catch (IOException e) {
Log.e("Exception", "File write failed: " + e.toString());

then when I'm open my file (msydfile.txt) its just contains a,b,contour area (like : 15, 4.55, 8418) ..But i want to save others contour area like sequential data.

Hopefully, someone has some experience in this. Thanks..

Answer Source

if you want to append your data you have to tell the OutputStream to do so:

boolean append = true;
FileOutputStream fOut = new FileOutputStream(file, append); 

see FileOutputStream for further explanation.