I need to store three pairs of floats (three points coordinates) in file, then read them and compare. I tried this way:
public Path loadPath()
{
Path path = new Path();
float x, y;
try
{
FileInputStream fis = new FileInputStream(filePath);
DataInputStream dis = new DataInputStream(fis);
for(int i = 0; i < 3; i++)
{
x = dis.readFloat();
y = dis.readFloat();
path.addCircle(x, y, rad, Path.Direction.CW);
}
dis.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
return path;
}
public void savePath(Path cPath)
{
PathMeasure pm = new PathMeasure(cPath, false);
float coords[] = {0f, 0f};
try
{
FileOutputStream fos = new FileOutputStream(filePath);
DataOutputStream dos = new DataOutputStream(fos);
do
{
pm.getPosTan(pm.getLength() * 0.5f, coords, null);
dos.writeFloat(coords[0]);
dos.writeFloat(coords[1]);
}
while(pm.nextContour());
dos.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
But DataOutputStream
writes in binary format and I get this in my file:
BК CF BК CF BК CF
So when DataInputStream
tries read this, it gets something strange.
I also tried to write with FileWriter
, file contents looks fine, but I can't propertly read floats out there.
What need I use to write/read floats property?