0

파일에 3 쌍의 플로트 (좌표 3 점)를 저장 한 다음 비교하여 비교해야합니다. 나는이 방법을 시도 :내부 저장 장치에서 플로트 쓰기 및 읽기

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으로 작성하려고 시도했지만 파일 내용이 정상적으로 보였지만 제대로 읽을 수 없습니다.

나는 floats 속성을 쓰거나 읽는데 무엇이 필요합니까?

+0

SharedPreferences가 충분하지 않습니까? – ozbek

+0

DataOutputStream은 데이터를 스트림 (및 경우에 따라 파일)에 직렬화하는 데 사용됩니다. writeUTF(), writeShort() 또는 writeFloat()가 전달하는 것과 관계없이 바이트 스트림으로 변환됩니다. 1.23, 1.01, 234.9989 등과 같이 사람이 읽을 수있는 파일을 요구하고 있습니까? – britzl

+0

@shoe 쥐, 아니요, SharedPreferences는 앞으로 더 많은 포인트를 사용할 것이기 때문에 접근하지 않습니다. –

답변

0

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