从内部存储器中写入和读取浮点数



我需要在文件中存储三对浮点数(三个点坐标),然后读取它们并进行比较。我试过了:

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

但是DataOutputStream以二进制格式写入,我在我的文件中得到这个:

BК CF BК CF BК CF

所以当DataInputStream尝试读取这个时,它会得到一些奇怪的东西。我也试着用FileWriter写,文件内容看起来很好,但我不能正确地读取浮点数。

我需要用什么来写/读浮动属性?

使用java中的扫描器和格式化器。util图书馆。您可以使用此对象创建和写入多个数据类型到文件。下面是一个例子:

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

相关内容

  • 没有找到相关文章

最新更新