gpt4 book ai didi

java - 在Java中保存顺序数据

转载 作者:太空宇宙 更新时间:2023-11-04 12:07:14 25 4
gpt4 key购买 nike

我在保存顺序数据(轮廓区域)时遇到一些问题,我有一个检测轮廓的项目。我想在我的 android 中保存数据轮廓区域。我已经将该轮廓区域保存在文件中,但它只是最后一个,而不是顺序数据(所有轮廓区域)。这是我的轮廓代码:

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;
}

这是我保存文件的代码:

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

这是 writeToFile 函数:

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

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

然后,当我打开文件(msydfile.txt)时,它只包含 a、b、轮廓区域(如:15、4.55、8418)..但我想保存其他轮廓区域,例如顺序数据。

希望有人在这方面有一些经验。谢谢..

最佳答案

如果您想附加数据,您必须告诉 OutputStream 这样做:

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

参见FileOutputStream以获得进一步的解释。

关于java - 在Java中保存顺序数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40231389/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com