2014-02-09 29 views
1

我在讀取一些文件時遇到了一些小問題。我正在製作遊戲,並決定爲地圖製作自己的文件類型。我製作了一個特殊的應用程序來製作這些地圖文件。一旦我實例化地圖,我可以選擇調用readFile(String path)將地圖設置爲保存的地圖。我知道我必須以相同的順序讀取和寫入流,並且一切都很順利,直到我添加了關於讀取和寫入byte [] []的語句。我無法弄清楚爲什麼我得到這個異常,以及如何仍然讀取一個字節[] []。這是我的班。使用readObject獲取字節[] []給出OptionalDataException

public class Map implements Serializable{ 

    String savePath; 
    int boxWidth; 
    int boxHeight; 
    int mapWidth; 
    int mapHeight; 
    BufferedImage map; 
    byte[][] encoded; 
    LinkedList<BufferedImage> tileSet = new LinkedList<BufferedImage>(); 

    Map(int boxWidth, int boxHeight, int mapWidth, int mapHeight){ 
     map = new BufferedImage(boxWidth * mapWidth, boxHeight * mapHeight, BufferedImage.TYPE_INT_RGB); 
     Graphics g = map.createGraphics(); 
     g.setColor(Color.WHITE); 
     g.fillRect(0, 0, map.getWidth(), map.getHeight()); 
     g.dispose(); 
     this.boxHeight = boxHeight; 
     this.boxWidth = boxWidth; 
     this.mapHeight = mapHeight; 
     this.mapWidth = mapWidth; 
     initEncode(); 
    } 

    Map(){ 
     map = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB); 
     this.boxHeight = 0; 
     this.boxWidth = 0; 
     this.mapHeight = 0; 
     this.mapWidth = 0; 
     initEncode(); 
    } 

    void initEncode(){ 
     int width = 2 * mapWidth + 1; 
     int height = 2 * mapHeight + 1; 
     encoded = new byte[width][height]; 
     for(int i = 0; i < width; i++){ 
      for(int j = 0; j < height; j++){ 
       encoded[i][j] = 0; 
      } 
     } 
    } 

    void setMapTile(int i, int j, byte index){ 
     encoded[2 * i + 1][2 * j + 1] = index; 
    } 

    void setMapWall(int i, int j, byte index){ 
     encoded[2 * i][2 * i] = index; 
    } 

    void addToTileset(Tile tile){ 
     tileSet.add(tile.tile); 
     writeFile(savePath); 
    } 

    //writing to file with path - boolean is for whether it went successfully or not 
    boolean writeFile(String path){ 
     savePath = path; 
     try{ 
      OutputStream file = new FileOutputStream(path); 
      OutputStream buffer = new BufferedOutputStream(file); 
      ObjectOutputStream output = new ObjectOutputStream(buffer); 

      writeObject(output); 

      output.close(); 
      buffer.close(); 
      file.close(); 
     }catch(IOException ex){ 
      System.err.println("Could not Write to file: " + path + "\nError caused by: " + ex); 
      return false; 
     } 
     return true; 
    } 

    //reading from file with path - boolean is for whether it went successfully or not 
    boolean readFile(String path){ 
     savePath = path; 
     try{ 
      InputStream file = new FileInputStream(path); 
      InputStream buffer = new BufferedInputStream(file); 
      ObjectInputStream in = new ObjectInputStream(buffer); 

      readObject(in); 
      initEncode(); 

      file.close(); 
      buffer.close(); 
      in.close(); 
     }catch(IOException ex){ 
      System.err.println("Could not read from file: " + path + "\nError caused by: " + ex + "\n"); 
      ex.printStackTrace(); 
      return false; 
     }catch(ClassNotFoundException e){ 
      System.err.println("Could not read from file: " + path + "\nError caused by: " + e + "\n"); 
      e.printStackTrace(); 
     } 
     return true; 
    } 

    private void writeObject(ObjectOutputStream out) throws IOException { 
     out.writeInt(boxHeight); 
     out.writeInt(boxWidth); 
     out.writeInt(mapHeight); 
     out.writeInt(mapWidth); 

     ImageIO.write(map, "png", out); 
     out.writeObject(encoded); 

     out.writeInt(tileSet.size()); 
     for(BufferedImage b: tileSet){ 
      ImageIO.write(b, "png", out); 
     } 
    } 

    public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{ 
     boxHeight = in.readInt(); 
     boxWidth = in.readInt(); 
     mapHeight = in.readInt(); 
     mapWidth = in.readInt(); 

     map = ImageIO.read(in); 
     encoded = (byte[][]) in.readObject(); 

     int tileSetSize = in.readInt(); 
     for(int i = 0; i < tileSetSize; i++){ 
      tileSet.add(ImageIO.read(in)); 
     } 
    } 
} 

是否有某種原因,我的(字節[] [])的readObject()行拋出OptionalDataException以及如何我還是讀/寫我的byte [] []。

編輯:謝謝你的回答Abhinav庫馬爾。我忽略了這一點,但是當我修復代碼時,它仍然在同一行發生同樣的錯誤。 (這個班現在已經修好了)。

+0

檢查'OptionalDataException' javadoc。是'eof'還是'length'設置? – SJuan76

回答

0

你必須閱讀的InputStream以相同的順序和相同的格式,在信息流中寫,否則你會得到OptionalDataException

你的順序寫在OutputStream的數據: -

 ImageIO.write(map, "png", out); 
     out.writeInt(2 * mapWidth + 1); 
     out.writeObject(encoded); 

而你正在閱讀的流中的順序: -

 map = ImageIO.read(in); 
     encoded = (byte[][]) in.readObject(); 

剛纔看了INT後ü閱讀map.The正確的代碼是: -

public void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException{ 
     boxHeight = in.readInt(); 
     boxWidth = in.readInt(); 
     mapHeight = in.readInt(); 
     mapWidth = in.readInt(); 
     map = ImageIO.read(in); 
     in.readInt();// you read this int and assign it to the object as you wish 
     encoded = (byte[][]) in.readObject(); 
     int tileSetSize = in.readInt(); 
     for(int i = 0; i < tileSetSize; i++){ 
      tileSet.add(ImageIO.read(in)); 
     } 
    } 
相關問題