Search code examples
androidfileinputstreamfileoutputstreamdatainputstreamdataoutputstream

Write and read floats from internal storage


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?


Solution

  • Use a Scanner and a Formatter from the java.util library. You can use this object to create and write multiple data types to files. Here's an example:

    package pckge.name.goes.here;
    
    import java.io.*;
    import java.util.*;
    
    public class Main {
    
        private static Formatter writer;
        private static Scanner input;
    
        private static void openFileForWriting(String file) {
            try {
                writer = new Formatter(file);
            } catch(Exception e) {
                System.err.println("Couldn't create file!");
            }
        }
    
        private void openFileForReading(String file) {
            try {
                input = new Scanner(new File(file));
            } catch(Exception e) {
                System.err.println("Couldn't find file!");
            }
        }
    
        private void closeFileForWriting() {
            writer.close();
        }
    
        private void closeFileForReading() {
            input.close();
        }
    
        private void writeFloat(float f) {
            writer.format("%f%n", f);
        }
    
        private float[] readCooridnates(String xyz) {
            float[] coordinates = new float[2];
            int position = 0;
    
            while(scanner.hasNext()) {
                switch(xyz) {
                case "x":
    
                    switch(position) {
                    case 0:
                        coordinates[position] = Float.parseFloat(scanner.next());
                        break;
                    case 1:
                        coordinates[position] = Float.parseFloat(scanner.next());
                        break;
                    default:
                        //Do nothing
                        break;
                    }
    
                    break;
                case "y":
                    switch(position) {
                    case 2:
                        coordinates[position] = Float.parseFloat(scanner.next());
                        break;
                    case 3:
                        coordinates[position] = Float.parseFloat(scanner.next());
                        break;
                    default:
                        //Do nothing
                        break;
                    }
                    break;
                case "z":
                    switch(position) {
                    case 4:
                        coordinates[position] = Float.parseFloat(scanner.next());
                        break;
                    case 5:
                        coordinates[position] = Float.parseFloat(scanner.next());
                        break;
                    default:
                        //Do nothing
                        break;
                    }
                    break;
                default:
                    //Do nothing
                    break;
                }
    
                position++;
            }
    
            return coordinates;
        }
    }